Your question is Normalize Sensor Data And Detect Anomalies. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
MarshBerry's monitoring pipeline receives chronological sensor readings as timestamped numeric values. Implement a function that min-max normalizes all readings and identifies values that are anomalous compared with the preceding readings.
For each reading, use up to the previous window readings as its baseline. A reading is anomalous when the baseline contains at least two values and either the baseline standard deviation is zero while the current value differs from the baseline mean, or the absolute difference from the baseline mean is greater than threshold * standard_deviation. Use population standard deviation. The current reading must not be included in its own baseline.
Return a dictionary with normalized, a list of normalized values rounded to four decimal places, and anomalies, a list of zero-based indices. Normalize using (value - minimum) / (maximum - minimum). If all values are equal, every normalized value is 0.0.
Input readings is a non-empty list of dictionaries with integer timestamp and numeric value fields. Input timestamps are chronological. window is a positive integer, and threshold is a positive number. Return {"normalized": list[float], "anomalies": list[int]}.
def normalize_and_detect(readings, window, threshold):