Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-319

Weighted Moving Average Endpoint

Problem

Compute a weighted average of the last k observations, where k is the length of the weights list. The oldest observation in the window is paired with weights[0] and the newest with weights[-1]; the result is the dot product divided by the sum of the weights.

Return 0.0 when the weights list is empty, the weights sum to zero, or the weights list is longer than the series.

Hint

Use the most recent k values only and normalize by the total weight.

Test cases

case 1
in: [[1,2,3,4],[1,2]]
expected: 3.6666666666666665
case 2
in: [[10,20,30],[1,1,1]]
expected: 20
case 3
in: [[1,2],[0,0]]
expected: 0
case 4
in: [[1,2,3],[1,1,1,1]]
expected: 0
Starter codePython
def wma_endpoint(series, weights):
    # Your code here
    pass

4 test cases · run your code against them in the editor