Median


#1

def median(x):
orderd_list = sorted(x)
length = len(x)
print length
index1 = length // 2
if length % 2 == 0:
index2 = index1 - 1
result = (x[int(index1)] + x[int(index2)]) / 2.0
else:
result = x[index1]
return result

This is my code but I get error:
median([4, 5, 5, 4]) returned 5.0 instead of 4.5
Please help!


#2

its because you sort x and assign it to orderd_list, but then you never use orderd_list, you keep using x


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.