15. Median


#1


What am I doing wrong?

def median(l):
    l1=sorted(l)
    if len(l1)%2==0:
        return (l1[(int(len(l1)/2)-1)]+l1[int(len(l1)/2)])/2
    elif len(l1)%2!=0:
        return (l1[int((len(l1)/2)-0.5)])

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


#2

==> (l1[(int(len(l1)/2)-1)]+l1[int(len(l1)/2)])/2.0

int to float

print 9 / 2 # 4
print 9 / 2.0 or print 9.0 / 2 or print 9.0 / 2.0 # 4.5


#3

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