Problem 190651 · medium · Level 01 Prerequisites & Setup

One Typo, Two Averages

mean · median · outliers · robustness · lists

A teacher typed a class's quiz marks into a spreadsheet and later found one typo: the mark at position index should have been corrected. Before re-sending the report she wants to know how much the typo had distorted the two averages she quoted: the mean (total divided by count) and the median (the middle of the sorted marks, or the mean of the two middle marks for an even count).

Write typo_effect(marks, index, corrected) that returns the tuple (mean_change, median_change), where each change is the value after the correction minus the value before it. The list marks itself must not be changed.

Examples

Input:  marks = [14, 16, 12, 81, 15], index = 3, corrected = 8
Output: (-14.6, -1)
Explanation: before, the mean is 138 / 5 = 27.6 and the median is 15;
after, the marks are [14, 16, 12, 8, 15], the mean is 65 / 5 = 13.0 and the median is 14.

Input:  marks = [70, 9, 8, 6], index = 0, corrected = 7
Output: (-15.75, -1.0)
Explanation: the median goes from (8 + 9) / 2 = 8.5 to (7 + 8) / 2 = 7.5.

Constraints

  • 1 <= len(marks) <= 10**5, 0 <= index < len(marks)
  • every mark is a whole number from 0 to 10**4 (a typo can be far outside the real range); corrected is a whole number from 0 to 100
  • answers are compared with a small tolerance

Goals

  • Measure how far one wrong value moves the mean and the median
  • Correct a copy of the data without changing the original
  • See in numbers why the median resists outliers
Starting Python…