Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-034

Winsorize List

Problem

Return data with the k smallest values replaced by the (k+1)-th smallest and the k largest replaced by the (k+1)-th largest, preserving the original order. If 2*k >= n, replace every element with the median; if k <= 0, return the values unchanged. Return [] for empty input.

Hint

Bound each value between the two clipping thresholds.

Test cases

case 1
in: [[1,2,3,4,5,100],1]
expected: [2,2,3,4,5,5]
case 2
in: [[1,2,3,4],1]
expected: [2,2,3,3]
case 3
in: [[5,1,4,2],0]
expected: [5,1,4,2]
case 4
in: [[1,10],1]
expected: [5.5,5.5]
case 5
in: [[],1]
expected: []
Starter codePython
def winsorize(data, k):
    # Your code here
    pass

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