Median...cant find whats wrong with my code!


#1




Replace this line with your code. 
ef median(numbers):
    nsorted = sorted([numbers])
    total = 0
    ln = len(nsorted)
    if ln % 2== 0:
        num1 = nsorted[ln/2.0]
        num2 = nsorted[(ln/2.0) - 1]
        total += (num1 + num2)/2.0
        return total
    else:
        total += numbers[ln/2.0]
        return total


#2

here:

total += numbers[ln/2.0]

by dividing by 2.0, this index becomes a float. Indexes can only be integers


#3

how can I fix it without the result being an integer instead of a float?
i feel stupid now :sweat_smile:


#4

5 / 2 = 2
5 / 2.0 = 2.5

#5

but I can't use a float in the division as you said... than where do I divide by 2.0 if I can't do it like I did?


#6

float division:

5 / 2.0 = 2.5

integer division:

5 / 2 = 2

when you try access list elements by index, you need integers. So here:

        num1 = nsorted[ln/2.0]
        num2 = nsorted[(ln/2.0) - 1]
        total += numbers[ln/2.0]

is where you can't use float division, because the results needs to be a integer


#7

yes, I removed those and left the:

but that still gets me the same error:
Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5

I feel really stupid but I dont understand how i can change the divisions in such a way that it will result in a float instead of an int


#8

can i see an updated version of your code? You did make some change, and you ran into a new problem


#9

def median(numbers):
    nsorted = sorted([numbers])
    total = 0.0
    ln = len(nsorted)
    if ln % 2 == 0:
        num1 = nsorted[(ln/2) + 1]
        num2 = nsorted[ln/2]
        total += (num1 + num2)/2.0
        return total
    else:
        total += numbers[ln/2]
        return total

#10

this is your code with some print statements:

def median(numbers):
    nsorted = sorted([numbers])
    print nsorted
    print len(nsorted)
    total = 0.0
    ln = len(nsorted)
    if ln % 2 == 0:
        num1 = nsorted[(ln/2) + 1]
        num2 = nsorted[ln/2]
        total += (num1 + num2)/2.0
        return total
    else:
        total += numbers[ln/2]
        return total
print median([4,5,5,4])

take a look at nsorted and the length of nsorted. Because nsorted is multi-dimensional, the outer list has only one item, so length is always 1, regardless of the list you pass as argument on function call


#11

I thought I could fix it by removing the ln variable and replacing it with een(numbers) but that only made things worse...
def median(numbers):
nsorted = sorted([numbers])
total = 0.0
if len(numbers) % 2 == 0:
num1 = nsorted[(len(numbers)/2) + 1]
num2 = nsorted[len(numbers)/2]
total += (num1 + num2)/2.0
return total
else:
total += numbers[len(numbers)/2]
return total

print median([4,5,5,4])


#12

that is a bad idea. The problem is in nsorted that is a multi-dimensional list, this happens the moment you sort the list, maybe you should troubleshoot that problem?


#13

I finally got it to work!!!
Thank you so so much for your patience! you are a hero :pray:


#14