Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-017

Rolling Mean Window k

Problem

Compute the simple moving average of a series with window size k. Each output is the mean of a contiguous window, so the result has len(series) - k + 1 entries.

Return an empty list if k is not positive or k is larger than the series.

Hint

Slice the series from i to i + k for each window start.

Test cases

case 1
in: [[1,2,3,4,5],3]
expected: [2,3,4]
case 2
in: [[1,2],1]
expected: [1,2]
case 3
in: [[1,2,3],3]
expected: [2]
case 4
in: [[1,2],0]
expected: []
Starter codePython
def rolling_mean(series, k):
    # Your code here
    pass

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