15/15 Median([4, 5, 5, 4]) returned 6.5 instead of 4.5 error


#1

def median(items):
itemindex = len(items)
organised_list= sorted(items)
#print (organised_list)
if itemindex % 2 == 0:
evenindex = itemindex / 2
evenindex2 = evenindex - 1
evenmedian = (organised_list[evenindex2] + organised_list[evenindex] / 2.0)
return evenmedian
else:
return organised_list[itemindex/2]

lawd = [1,5,4,2,7,2,1,8]
print median(lawd)

i have no clue why this isn't working. A speedy response would be greatly appreciated :smile:


#2

you have misused parentheses
evenmedian = (organised_list[evenindex2] + organised_list[evenindex] ) /2

1 + 1 / 2 = 1.5
(1 + 1 / 2 ) = 1.5
(1 + 1 ) / 2 = 1


#3

def median(numbers):
sequence=[]
sequence=sorted(numbers)
a=len(sequence)
#float b
if a%2==0:
# b=(sequence[a/2-1]+sequence[a/2-1+1])/2
return (sequence[a/2-1]+sequence[a/2-1+1])*0.5
else:
return sequence[a/2-1+1]


#4

you are really smart. thank you soo much for posting your code. This problem was really hard for me


#5

I have the same problem. Can someone explain what's wrong with this code?
def median(t):
t = sorted(t)
f = len(t)
s = f / 2
if f % 2 == 0:
g = t[s]
v = s - 1
k = t[v]
l = g + k
u = l / 2
return u
elif f == 1:
return t[f-1]
else:
a = int(s + 0.5)
n = f-a
c = t[n]
return c

The error was Median([4, 5, 5, 4]) returned 4 instead of 4.5.
Thanks for your help!


#7
def median(lista):
lista=sorted(lista)    
tam=len(lista)    
med=0
med1=0
prom=0
if tam==1:
    med=0
    return lista[med]
elif tam%2==0:
    med=tam/2
    med1=med-1
    prom=(lista[med] + lista[med1])/float(2)
    return prom
else:
    med=tam/2
    return lista[med]

print median([ 1])


#8

This was about as short as I could make it.
def median(list1):
list1.sort()
if len(list1) <= 1:
return list1[0]
elif len(list1) % 2 == 1:
return list1[len(list1)/2]
else:
return (list1[len(list1)/2] + list1[len(list1)/2 - 1]) / 2.0


#9

no problem my brudda just needed some help just keep practicing and you'll get there. You have to want to be good more than anything else.


#10

thanks... i took 15 min tryin out


#11

my code is:
def median(num_list):
len1=len(num_list)
ls=[]
if len1==1:
med=num_list[0]

else:

    ls=sorted(num_list)
    len1=len(ls)
    if len1%2==0:
        i1=len1/2
        i2=(len1/2 )-1
        med=(ls[i1]+ls[i2])/2.0
    else:
        i=int(len1/2 +1)
        med=ls[i]

return med

it does not sorted out list::

it is showing::Oops, try again. median([6, 8, 12, 2, 23]) returned 12 instead of 8


#12

Here's my code:

def median(numbers):
numbers_sorted = sorted(numbers)
middle = float(len(numbers)/2.0)
if (len(numbers))%2 == 0:
median_number = float (numbers_sorted[int(middle-1)] + numbers_sorted[int(middle)])/2
else:
median_number = numbers_sorted[int(middle)]
return median_number


#13

Here's my code:

def median(sequence):
size = len(sequence)
Oseq = sorted(sequence)

if size%2 != 0:
    med = Oseq[int(size/2.0)]
else:
    mid = int(round(size/2.0))
    num1 = Oseq[mid-1]
    num2 = Oseq[mid]
    med = (num1+num2)/2.0
return med

print median([4,5,5,4])
print median([7,12,3,1,6])


#14

i dont seem to understand your last line could you please explain


#15

that means if the list has a even number of items, take the item with index of half of the number (here is len(list1)/2) and the item right before that one (which is len(list1)/2-1 )and then take average of these two


#17

Here's my code. I'm pretty happy/proud how it turned out and i think its very easy to read which is always what i'm going for. If anybody has anything they think would make this easier let me know!

def median(numbers):
sorted_numbers = sorted(numbers)
parity = "test"

if len(sorted_numbers) % 2 == 0:
    parity = "even"
else:
    parity = "odd"

if parity == "odd":
    return sorted_numbers[len(sorted_numbers)/2]
else:
    return float(sorted_numbers[len(sorted_numbers)/2]+sorted_numbers[(len(sorted_numbers)-1)/2])/2

#18

def median(lisst):
s = sorted(lisst)
if (len(s) % 2) == 0:
ni = (len(s) % 2.0)
nl = ni + 1
return (ni + nl) / 2
else:
return (len(s)/2.0)

This is my code it's a little different syntax than everyone elses, but its the same idea. I keep getting the error code:
Oops, try again. median([1]) returned 0.5 instead of 1


#19

def median(list1):
list2 = list(list1)
list2.sort()
a = len(list2)/2
if a % 2 != 0:
return list2[a - 0.5 + 1]
elif a % 2 == 0:
return float((list2[(a-1)] + list2[a])/2)

Could anyone explain me why this not work? The message is:

"Oops, try again. median([4, 5, 5, 4]) returned 4.0 instead of 4.5" Thanks in advance;)


#20

Hi again, I found the answer myself. First of all, the result of simple computations is different than intuition says. So, it is really useful to know what the compiler 'thinks'. In order to get it I used this tool:

http://www.pythontutor.com/visualize.html#mode=edit

I recommend:)

Tomas