15.median


#1

I got correct result on my local IDE but was told wrong on the platform.

My code is returning 4.5 not 4, but the platform keeps telling me that the result is 4.

Can anyone help me to check this inconsistency? Thank you very much!

def median(List):
    mylist = sorted(List)
    if len(List) == 1:
        return List[0]
    elif len(List)%2 == 0:
        return mylist[int(len(mylist)/2-1)]/2 + mylist[int(len(mylist)/2)]/2 
    else: 
        return mylist[len(List)/2-0.5]

#2

Your local IDE may be Python 3, whereas this lesson is in Python 2 which does not return a float from every quotient. We need to be explicit about floats.

float(x[m - 1] + x[m]) / 2

#3

Conversely, in Python 3 we need to be explicit about ints.


#4

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