Problem 294371 · medium · Level 02 Linear Data Structures

The Numbers Behind a Box Plot

quartiles · interquartile range · outliers · median · sorting

A cycling club draws a box plot of its members' commute times (minutes). The drawing needs these numbers, computed from the sorted list of times with the club's halves method:

  • median: the middle value of the sorted list (the mean of the two middle values when the length is even).
  • q1: the median of the lower half, the values before the middle. When the length is odd, the middle value itself belongs to neither half. q3: the median of the upper half, the values after the middle. With a single value, q1 and q3 are that value.
  • iqr = q3 - q1, the low fence q1 - 1.5 * iqr and the high fence q3 + 1.5 * iqr.
  • outliers: every value strictly below the low fence or strictly above the high fence, in increasing order (repeated values repeated).
  • low_whisker and high_whisker: the smallest and the largest value that are not outliers.
  • min and max: the smallest and largest value overall.

Write box_numbers(times) that returns a dictionary with exactly the keys "min", "q1", "median", "q3", "max", "iqr", "low_whisker", "high_whisker" and "outliers". The list times must not be changed.

Examples

Input:  times = [22, 25, 19, 31, 24, 58, 27, 21, 26]
Output: {"min": 19, "q1": 21.5, "median": 25, "q3": 29.0, "max": 58, "iqr": 7.5,
         "low_whisker": 19, "high_whisker": 31, "outliers": [58]}
Explanation: sorted: 19 21 22 24 [25] 26 27 31 58. The lower half 19 21 22 24 has median 21.5,
the upper half 26 27 31 58 has median 29. The fences are 21.5 - 11.25 = 10.25 and
29 + 11.25 = 40.25, so only 58 is outside them.

Input:  times = [3, 9, 4, 7, 5, 6]
Output: {"min": 3, "q1": 4, "median": 5.5, "q3": 7, "max": 9, "iqr": 3,
         "low_whisker": 3, "high_whisker": 9, "outliers": []}
Explanation: an even length splits into 3 4 5 and 6 7 9.

Constraints

  • 1 <= len(times) <= 10**5
  • every time is a whole number with 0 <= time <= 1000
  • numbers may be returned as int or float; they are compared with a tolerance of 1e-6

Goals

  • Compute quartiles with a precisely stated method (medians of the two halves)
  • Build the 1.5 × IQR fences and flag the values outside them
  • Find where a box plot's whiskers end
Starting Python…