# 15/15 what is wrong?

#1

Hey guys!

It keeps telling me that the answer to [4 5 5 4] is 4.0 but I can´t see why.

#2

Worst.. I tried this and it worked... I don't know why

#3

Could you copy paste your code the forum so i can run it? Since there are many different solutions to this problem, i need to figure out your code, which is really time consuming from a image

#5

Sure, here you go. Why does this work it makes no sense to me... i just added stuff out of anger

``````def median(lista):
lista = sorted(lista)
nu_nu = len(lista)
if nu_nu % 2 == 0:
in1 = (nu_nu / 2) - 1
in2 = in1 + 1
nu1 = lista[in2]
nu2 = lista[in1]
ni = float((nu1 + nu2)/2) + 0.5
return ni
else:
ni = (nu_nu /2) + 0.5
ni = int(ni)
return lista[ni]``````

#6

Which bit do you not understand? You get the length of the list, if the list has a odd number of items, you get the middle item by doing:

``ni = (nu_nu /2) + 0.5``

which you can simplify to:

``ni = nu_nu /2``

a int (nu_nu) divide by a int (2) is a int. then you can remove the next line:

``ni = int(ni)``

If the list had even numbers (lets say 4), in1 is 4/2 - 1 which is 1 (which is the second item, counting starts from zero) and in2 is the item next to it, then you get the values at these indexes, you do some math, and the right number gets returned, you might want to add a function call at the end:

``print median([1])``

this way, you can add some print statements in your function to see what it does

#7

Ok I see where is was having trouble.

Why is

ni = (nu_nu /2) + 0.5

the same as

ni = nu_nu /2

??

#8

well, first off, you can just remove the brackets:

``nu_nu/2 + 0.5``

other of operations, dividing is before sum (plus, we work from left to right). now, nu_nu is the length of the list (lets say it is 5):
5/2 = 2 (2.5, but a int divided by a int is a int, unless you specify different), so then you add a half (0.5), now it is 2.5, and then you cast it to a int, which makes it two again. So you take extra steps to come up with the same number, it is the same when you do the math for nu_nu is 7 (7/2 = 3 then 3 + 0.5 = 3.5 then cast to a int and is 3 again), same for 9, 11 and you can go on. So it the same, sorts off

#9

Ok got it, I didn't get the int/int thing before. Thanks!!