Median


#1

Median:

I get an error with my code with the following:

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

When I run my code in idle, I get 8. I'm not sure what's wrong with it.

def median(lst):
    newLst = sorted(lst)
    if len(newLst) % 2 == 0:
        middle = int(len(newLst) / 2)
        x = newLst[middle]
        y = newLst[middle - 1]
        return (x + y) / 2.0
    else:
        med = int(len(newLst) / 2 - 0.5)
        x = newLst[med]
        return x

#2

which version does your python idle run?

i think the problem is here:

med = int(len(newLst) / 2 - 0.5)

the list you have a problem with has a length of 5, your idle runs python3, the exercise is python2:

# python2
5 / 2 # 2
# python3
5 / 2 # 2.5

so there is a difference, so in the exercise 2 - 0.5 (which is 1.5) converted to integer is 1, that is not the right index


#3

AHHHHH thank you!!! Yea I thought I was in my vmware fusion box running my idle which is v2. Thank you for the heads up.


#4