Median. Whats wrong with my code?


#1

import math
def median(seq):
    if len(seq)%2==0:
        new=sorted(seq)
        l=len(seq)
        med=(new[math.floor(l/2)]+new[math.floor(l/2-1)])/2
        return med
    else:
        new=sorted(seq)
        l=len(seq)
        med=new[math.floor(l/2)]
        return med

#2

Hello, @nilabhra!
The thing with the floor function is that it will return the floor of a given parameter as a float where it will be the largest integer value less than or equal to x.
And since it's returning a float value, you won't be able to access a given position on your list.
Float values are all values who have the decimal part, like 1.3415.

But can you still use it since it will return a float value? Yes, use the function int() on your math.floor(l/2) where you'll put the last statement inside of int.

Also, the median of a value can be a float.
If it happens you get [1,2,3,4], you're median should return 2.5 but not 2.


#3

when dividing a integer by a integer:

new[l/2]

the outcome will be a integer,all decimals will be thrown out of the window. No need to use floor, floor will return a integer, which you can NOT use to access items in a list


#4

Hi, i can't fix it!!:slightly_smiling:
Codeacademy mess:
"Oops, try again.
median([4, 5, 5, 4]) returned 4 instead of 4.5"

but if i run the script on my IDLE it return 4.5

def median(lista):
    sorl = sorted(lista)
    if len(lista)==1:
        med = lista[0]
        return med
    elif len(lista)%2==0:
        a = int((len(sorl)/2)-1)
        b = int(a + 1)
        med = ((sorl[a]+sorl[b])/2)
        return med
    else:
        a = int(len(sorl)/2-0.5)
        med = sorl[a]
        return med

#5

If you make a function call:

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

this really return 4. the problem is in this line:

med = ((sorl[a]+sorl[b])/2)

sort[a] is 4, sort[b] is 5, which is a total of 9. Which you divide by 2. Both 9 and 2 are integers, so a integer is returned (every decimal number gets thrown out of the window) so it return 4. There are things you can do, change 2 to a float, a integer divided by a float returns a float, or you need to cast it to a float


#6

Ya...its working fine on my IDLE


#7

Yes, but which version of python is your IDLE running? It runs fine indeed on my python3.5 idle, however, codecademy uses 2.7.3, so please try to modify the code to make it work in the exercise (like i explained)


#8

def median(lista):
sorl = sorted(lista)
if len(lista)==1:
med = lista[0]
return med
elif len(lista)%2==0:
a = int((len(sorl)/2)-1)
b = int(a + 1)
med = ((sorl[a]+sorl[b])/2.0)
return med
else:
a = int(len(sorl)/2.0)
med = sorl[a]
return med


#9

wrote your code again by doing what stetim94 said and why did u subtract 2.0 by 0.5 in else part? just do these changes and ur code will work fine.. :slightly_smiling:


#10

Posting full answers without explanation is not according the guidelines, your answer is on the edge.

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard. Indent is quit crucial in python, if you do not format it will show up like it did (without any indent), not very practical if you try to help someone


#12

share my lines, do you have concise way?

def median(alist):
sorted_alist = sorted(alist)
if len(sorted_alist)%2 ==0:
return (sorted_alist[(len(sorted_alist)-1)/2] + sorted_alist[(len(sorted_alist)-1)/2+1])/2.0
else:
return sorted_alist[int((len(sorted_alist)-1)/2)]


#13

There are even shorter ways (which will get insane), i wrote this one a while back:

def median(x):
    x = sorted(x)
    return x[len(x)/2] if len(x)%2==1 else (x[len(x)/2] + x[len(x)/2-1]) /2.0

which is pretty short. Yours is also pretty short, nice job :slightly_smiling:


#14

@stetim94 great, that's what I want thanks!!!:clap:


#17

@aj121879, posting full answers without any explanation is not according to the guidelines, thank you for your understanding


#18

The below code works fine and seems to be more understandable wherein we convert the value to float type before finding median value

def median(l):
l1=sorted(l)
ln=len(l1)
if ln==1:
return l1[ln-1]
elif ln%2==0:
lx=float(l1[ln/2])
ly=float(l1[(ln/2)-1])
return (lx+ly)/2
else:
return l1[(ln-1)/2]
print median([1,9,8,3,7])


#19

#20

This issue is resolved, if you have a question, make your own topic.