15. Median - Even sequences


#1

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

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

I know my else statement is messed up, but right now I'm more worried about why my elif statement (the one that runs if the length of the sequence is even) is not working. You know, I got nothing but straight A's in AP Calculus and I love a good challenge, but this is seriously making me question my ability to do simple math.

def median(num):
    sorted(num)
    middle = 0
    if len(num) == 1:
        return num[middle]
    elif len(num) % 2 == 0:
        middle = len(num)/2
        middle2 = middle-1
        subaverage = num[middle] + num[middle2]
        average = subaverage/2.0
        return average
    else:
        middle = len(num)/2.0
        return num[middle-1]


#2

this has very little do with math, here:

sorted(num)

you sort your list, but you don't store the sorted list anywhere, so inside your elif you use an unsorted list


#3

So, like this:

def median(num):
    new = sorted(num)
    middle = 0
    if len(new) == 1:
        return new[middle]
    elif len(new) % 2 == 0:
        middle = len(new)/2
        middle2 = middle-1
        subaverage = new[middle] + new[middle2]
        average = subaverage/2.0
        return average
    else:
        middle = len(new)/2.0
        return new[middle-1]

except now I'm getting an Oops, try again. median([6, 8, 12, 2, 23]) resulted in an error: list indices must be integers, not float


#4

you could have saved yourself some trouble by simply updating num:

num = sorted(num)

here:

middle = len(new)/2.0

you try to calculate an index, which should be integers, so why divide by 2.0?


#5

I did it that way because in the instructions of the exercise it says: "Last but not least, note that (2 + 3) / 2 is not the same as (2 + 3) / 2.0! The former is integer division, meaning Python will try to give you an integer back. You'll want a float, so something like (2 + 3) / 2.0 is the way to go."


#6

but you are calculating an index here? So then you want an integer? Indexes of lists are integers, not floats.

this is for the sum of the middle two values divided by two.

I was talking about getting the middle index for a list with a odd number of items


#7

Ooh ok. Well, I did this:

def median(num):
    num = sorted(num)
    middle = 0
    if len(num) == 1:
        return num[middle]
    elif len(num) % 2 == 0:
        middle = len(num)/2
        middle2 = middle-1
        subaverage = num[middle] + num[middle2]
        average = subaverage/2.0
        return average
    else:
        middle = len(num)/2
        return num[middle-1]

But now I'm getting an Oops, try again. median([6, 8, 12, 2, 23]) returned 6 instead of 8


#8

you perfectly calculate the middle index for lists with odd number of items, but then you (for some reason) subtract one from the index when you get the value for the middle index


#9

I figured I would have to subtract 1 because python starts counting at 0 and my median would have been counting from 1 wouldn't it? Anyway, I did this:

def median(num):
    num = sorted(num)
    middle = 0
    if len(num) == 1:
        return num[middle]
    elif len(num) % 2 == 0:
        middle = len(num)/2
        middle2 = middle-1
        subaverage = num[middle] + num[middle2]
        average = subaverage/2.0
        return average
    else:
        middle = len(num)/2
        return num[middle]

And it worked...


#10

if you do the math:

1 / 2 = 0
3 / 2 = 1
5 / 2 = 2
7 / 2 = 3

you can see the length divided by two perfectly give the middle index


#11

... facepalm ...
How that never occurred to me, I have no idea.
But thank you so much for your help.


#12

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