15. Median


#1



Someone please help. I have no idea why my code doesn't work. I have gone through it so many times I can't figure it out.
The error I got is "Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5"


def median(aList):
    cloneList = sorted(aList)
    mid_length = len(cloneList)/2.0
    
    if (len(cloneList) % 2 == 0):
        return (cloneList[int(mid_length)] + cloneList[int(mid_length+1)])/2.0
    else:
        if (len(cloneList) == 1):
            return (cloneList[int(mid_length - 1)])
        elif (len(cloneList) > 1):
            return (cloneList[int(mid_length-0.5)])


#2

Change from plus to minus.


#3

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