Median


#1



Hi, can you please help me out with code below, not sure why it doesn't work...

Error message: "Oops, try again. median([4, 5, 5, 4]) returned 5 instead of 4.5"

Thanks


def median(integers):
    sorted(integers)
    if len(integers)%2==0:
        x = int(len(integers)/2)
        y = int(x-1)
        result = (integers[y]+integers[x])/2
    else:
        a = len(integers)/2
        b = int(a - 0.5)
        result = integers[b]
    return result


#2

SORTED...

def median(integers):
new_median = sorted(integers)
if len(new_median)%2==0:
x = int(len(new_median)/2)
y = int(x-1)
result = (new_median[y]+new_median[x])/2.0
else:
a = int(len(new_median)/2)
b = int(a + 0.5)
result = new_median[b]
return result


#3

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