All problems
Weighted Moving Average
Problem
Compute a weighted moving average where weights[0] applies to the most recent value of each window. For the window ending at index i the output is sum(weights[j] * x[i-j]) / sum(weights).
Return an empty list if weights is empty, longer than the series, or sums to zero. The output length is len(series) - len(weights) + 1.
Hint
Reverse the window when pairing it with the newest-first weights.
Test cases
case 1
in: [[1,2,3,4],[0.5,0.5]]
expected: [1.5,2.5,3.5]
case 2
in: [[10,20,30],[1,3]]
expected: [12.5,22.5]
case 3
in: [[5],[2]]
expected: [5]
case 4
in: [[1,2,3],[1,1,1,1]]
expected: []
Starter codePython
def weighted_moving_average(series, weights):
# Your code here
pass4 test cases · run your code against them in the editor