A bike-share company is cleaning a column of trip durations, in minutes, before training a model to predict how long trips take. A few entries look wrong: a 2-minute trip that is probably someone re-docking a bike, a 240-minute trip from a bike that was never returned properly. Rather than judge them by eye, the team applies the standard IQR rule.
Task: write iqr_fences(durations) and return a tuple (lower_fence, upper_fence, flagged):
flagged, the positions of the outlier trips in the original durations list, in increasing order. If there are none, it's an empty list.durations holds at least 4 numbers, in the order the trips were logged, not sorted.
The fences are built from and , which one wild value barely moves. That's why this rule is often preferred over the z-score rule, whose mean and standard deviation get dragged around by the very outliers they are supposed to catch.