Problem 288065 · medium · Level 02 Linear Data Structures

Peeling Off the Glitches

z-scores · outliers · standard deviation · iteration until stable

A weather station's temperature log contains the occasional glitch. The station's cleaning rule works in rounds:

  1. Take the readings that are still in the log. If fewer than 3 remain, stop.
  2. Compute their mean and population standard deviation sd. If sd is 0, stop.
  3. Flag every remaining reading x with abs(x - mean) > k * sd (strictly greater).
  4. If nothing was flagged, stop. Otherwise remove all flagged readings at once and start the next round.

A huge glitch inflates the standard deviation so much that a smaller glitch looks normal in the first round; only after the huge one is gone does the smaller one stand out.

Write peel_outliers(readings, k) that returns the sorted list of the indices (positions in readings) of every reading removed by the rule.

The setup provides station_log(n, seed), which returns n readings with a few glitches of different sizes.

Examples

Input:  readings = [12.1, 11.8, 12.4, 99.0, 12.0, 30.5, 11.9, 12.2, 12.3, 11.7], k = 2
Output: [3, 5]
Explanation: round 1: mean 22.59, sd 26.06; only 99.0 is more than 52.1 away.
Round 2 (without it): mean 14.1, sd 5.80; now 30.5 is 16.4 away, more than 11.6.
Round 3: mean 12.05, sd 0.229; nothing is more than 0.458 away, so the rule stops.

Input:  readings = [5, 5, 5, 5], k = 1
Output: []

Constraints

  • 0 <= len(readings) <= 2 * 10**4
  • 0.5 <= k <= 5
  • the tests contain no reading that lies exactly on the k * sd boundary

Goals

  • Flag values that lie more than k standard deviations from the mean
  • Recompute the mean and standard deviation after removing flagged values
  • See how one extreme value can hide a smaller one (masking)
Starting Python…