The average so far, after every reading
A quality chart plots not each reading but the average of everything measured up to that point, so a late wobble does not swing the line.
- Every reading is zero or more.
- After each reading, report the mean of every reading so far.
- Drop the fraction — the mean is rounded down to a whole number.
- An empty run of readings gives an empty result.
running_average(readings: list<int>) → list<int>
Where you start
def running_average(readings: list[int]) -> list[int]:
Worked examples
| Call | Result |
|---|---|
running_average([10, 20, 30]) | [10, 15, 20] |
running_average([1, 2]) | [1, 1] |
running_average([5]) | [5] |
running_average([]) | [] |
Hint
Carry the running total and divide by how many readings you have seen. Do not re-add the list each step.
Reference solution in Python
def running_average(readings: list[int]) -> list[int]:
out = []
total = 0
for i, reading in enumerate(readings):
total += reading
out.append(total // (i + 1))
return out