Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-054

Moving Average Forecast

Problem

Compute the trailing moving average of values with the given window size: the mean of each contiguous window of length window.

Return one average per complete window. If window is not positive or there are fewer values than the window, return [].

Hint

Slide a fixed-size window and average each slice.

Test cases

case 1
in: [[1,2,3,4,5],3]
expected: [2,3,4]
case 2
in: [[1,2],2]
expected: [1.5]
case 3
in: [[1,2],3]
expected: []
case 4
in: [[2,4,6,8],2]
expected: [3,5,7]
Starter codePython
def moving_average(values, window):
    # Your code here
    pass

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