Median


#1



Median


It's throwing error messages, as if the code is ridiculous, but it seems so right to me. Help! I believe the problem is with the brackets on the even and odd list number calculation, correct?

Example error: Oops, try again. median([6, 8, 12, 2, 23]) returned 10.0 instead of 8


def median(list):
    sorts = sorted(list)
    length = len(sorts)
    if length == 1:
        return 1
    if length % 2 == 0:
        return ((sorts[length/2]+ sorts[length/2-1]) / 2.0)
    else:
        return ((sorts[length/2] + sorts[length/2+1])/2.0)


#2

for a list like [6, 8, 12, 2, 23] which has an odd length, the median is simply the middle value of the sorted list


#3

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