All problems
Trimmed Mean
Problem
Return the mean after removing the k smallest and k largest values. Return 0.0 if data is empty or 2*k >= n. With k = 0 this is the ordinary mean.
Hint
Sort, slice off k values from each end, then average.
Test cases
case 1
in: [[1,2,3,4,5,100],1]
expected: 3.5
case 2
in: [[1,2,3,4,5],0]
expected: 3
case 3
in: [[10,1,9,2,8,3],2]
expected: 5.5
case 4
in: [[1,2],1]
expected: 0
case 5
in: [[],0]
expected: 0
Starter codePython
def trimmed_mean(data, k):
# Your code here
pass5 test cases · run your code against them in the editor