All problems
Rolling Median
Problem
Compute the rolling median of a series with window size k, which is far more robust to spikes than a rolling mean. Sort each window; for odd k take the middle value, for even k average the two middle values.
Return an empty list if k is not positive or k is larger than the series.
Hint
Two middle indices for even windows: k//2 - 1 and k//2.
Test cases
case 1
in: [[1,2,3,4,5],3]
expected: [2,3,4]
case 2
in: [[4,2,7,1],2]
expected: [3,4.5,4]
case 3
in: [[5],1]
expected: [5]
case 4
in: [[3,1,2],3]
expected: [2]
Starter codePython
def rolling_median(series, k):
# Your code here
pass4 test cases · run your code against them in the editor