Median


#1

error:
median([6, 8, 12, 2, 23]) returned 10.2 instead of 8

whit this list works:
median([4,5,5,4])

def median(x):
    l = float(len(sorted(x)))
    total = 0
    new = 0
    for u in x:
        total = total+u
        new = total/ l
        
    return new
print median([6, 8, 12, 2, 23])

#2

Your function looks to return an average, not a median. The median may be close to the average, but it is not the same kind of computed value.

A median is the middle value in a sorted sample if the length is odd; it is the average of the middle two values if the length is even.


#3

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