All problems
Centered Moving Average Series
Problem
Smooth a series with a centered moving average of odd window size k. For each position t from (k-1)//2 to n-1-(k-1)//2, average the k values centered on t.
Return an empty list when k is even, k is not positive, or k is larger than the series. A centered average cannot align with the series for even k, so those inputs are rejected.
Hint
The output is shorter than the input by k - 1 values.
Test cases
case 1
in: [[1,2,3,4,5],3]
expected: [2,3,4]
case 2
in: [[2,4,6,8,10],5]
expected: [6]
case 3
in: [[7],1]
expected: [7]
case 4
in: [[1,2,3,4],2]
expected: []
Starter codePython
def centered_moving_average(series, k):
# Your code here
pass4 test cases · run your code against them in the editor