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
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.
when dividing a integer by a integer:
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
Hi, i can't fix it!!
"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 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
If you make a function call:
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
Ya...its working fine on my IDLE
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)
sorl = sorted(lista)
med = lista
a = int((len(sorl)/2)-1)
b = int(a + 1)
med = ((sorl[a]+sorl[b])/2.0)
a = int(len(sorl)/2.0)
med = sorl[a]
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..
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
share my lines, do you have concise way?
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
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
The below code works fine and seems to be more understandable wherein we convert the value to float type before finding median value
This issue is resolved, if you have a question, make your own topic.