Median


#1


Please help! The error is"
Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5
thanks in advance!


def median(numbers):
    sorted_numbers=sorted(numbers)
    if len(sorted_numbers)%2==0:
        snd=len(sorted_numbers)/2
        fst=snd-1
        return (sorted_numbers[fst]+sorted_numbers[snd])/2
    elif len(sorted_numbers)==1:
        return sorted_numbers[0]
    else:
        odd=len(sorted_numbers)/2
        odd=round(odd)
        return sorted_numbers[odd-1]


#2

dividing an integer by integer will result an integer (if needs be, the number is floored)

to overcome this problem, use float (somehow, i will leave that for you to see if you can figure it out )


#5

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