runtime
409 / 500
question 409 NumPy Foundations · hard

A moving average of daily highs

A smoothed temperature line replaces each value with the average of a window of consecutive days. Write moving_average(values, window): return the mean of every run of window consecutive values, starting with the first full window. A window that doesn't fit gives []; a window smaller than 1 is a mistake: raise ValueError.

Functionmoving_average(values, window)
Returnsa list of len(values) - window + 1 floats; [] when window is larger than the list
RaisesValueError when window < 1
Examples
callmoving_average([-0.5, 6.7, 9.4, 7.2, 1.7], 3)returns[5.2, 7.766666666666667, 6.1]
callmoving_average([1, 2, 3, 4], 2)returns[1.5, 2.5, 3.5]
callmoving_average([22.2, 17.8], 5)returns[]
callmoving_average([1, 2, 3], 0)raisesValueError

Loading your progress…

A moving average of daily highs — Python practice · Runtime