Moving average without libraries

Mediumslicingrangeround· 20 XP

Daily sign-ups jump around a lot. A moving average smooths them by averaging each run of k days in a row.

Write a function moving_average(values, k) that returns the average of every run of k values in a row, each rounded to 2 decimals. A list of n values gives n - k + 1 averages, and if the list is shorter than k the answer is empty.

How it's checked

Your code runs, then the check below runs and its printed output is compared with ours.

  • moving_average([1, 2, 3, 4], 2) returns [1.5, 2.5, 3.5]

Hints, the later steps and the answer come with Pro.

def moving_average(values, k):
    ...