Median


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


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


why output get rounded ?


def median(x):
    q = sorted(x)
    if len(q)%2 == 0:
        u = len(q)/2 - 1
        l = len(q)/2
        fnl = (q[u] + q[l])/2
    else:
        fnl = q[int(round(len(q)/2))]
    return fnl
print median([4,5,5,4])


#2

python2 rounds (or rather floors) the output when the division involves two integers, maybe you could use float somehow?


#3

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