Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

List moving average

View list_ma.py
1 2 3 4 5 6 7 8 9 10 11
def moving_average(data_set, period=3):
avgs = []
 
for idx, item in enumerate(data_set[period-1:]):
avgs.append(round(sum(data_set[idx:period+idx]) / float(period), 2))
 
return avgs
 
data = [1, 2, 3, 6, 9, 12, 20, 28, 30, 25, 22, 20, 15, 12, 10]
 
assert moving_average(data, 3) == [2.0, 3.67, 6.0, 9.0, 13.67, 20.0, 26.0, 27.67, 25.67, 22.33, 19.0, 15.67, 12.33]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.