Median - a little confused?


#1

Hey guys..
so my code is:

but when I run it I get this error:
Oops, try again. median([1]) returned [1] instead of 1

What's causing it to return the result inside the [ ] brackets?


#2

because else will run? which returns num, which contains the sorted list

for odd lists (lists with odd number of items) the length of the list gives the middle index (this even goes for lists with one item):

1 / 2 = 0
3 / 2 = 1
5 / 2 = 2
7 / 2 = 3
9 / 2 = 4

and so on


#3


Am I heading in the right direction?
I still get an error where median([1]) returns 0.5 instead of 1 but I wrote (len(num)/2 + 0.5)


#4

Is there a problem with how I'm returning the result?


#5

I tried a bit of a different approach but essentially I've got the same issue.


#6

can you copy paste your code to the forum so i can run it?

on line 7, why + 0.5? i tried to tell you, you don't need this:


#7

def median(numbers): 
        num = sorted(numbers)
        x = 0 
        if len(num) % 2 == 0: 
                x = (len(num)/2 + len(num)/2 + 1)) / 2
        else:
                x = len(num)/2
        return x

edit: not sure why this doesn't indent when I type it up here


#8

i get an invalid syntax on this line:

x = (len(num)/2 + len(num)/2 + 1)) / 2

because the parentheses aren't good.

okay, now you calculate the middle index here:

x = len(num)/2

now you need to use the middle index to retrieve the middle value of the list


#9

for that first line, I've actually tried with different syntax a number of times but I get it wrong every time. Is it the parenthesis? Because If I want to divide the entire thing by 2 then I need the parenthesis to keep the order of operations that I want.

I'm not sure how to go about retrieving the middle value?


#10

true, but having more ) then ( will mean they don't match. That is a big problem.

you can use square brackets to retrieve values from list based on index, remember?


#11

oh wait.. didn't catch that sorry I thought I had equal ( and )


#12

I'm getting a syntax error on
else:
Why? I'm pretty sure I used it correctly in the above code


#13

ok fixed the else

Oops, try again. median([1]) returned None, did you remember to return the result? - so now I have to return the result wit the help of [ ] ?


#14

you still calculate the indexes, you need to use this indexes to retrieve values:

x = [1,2,3,4]
print x[0]

use 0 as index to retrieve value example


#15

Hey, sorry, I wasn't able to work on the code but I've been looking at it for the past half hour and I've got a question. Why is it len(num)/2-1 below the if statement and not len(num)/2 + 1?

Also I don't understand by what you meant with the last reply. I've tried writing print x[0] but it doesn't give me any response and I've tried a few other return and print statements with num/x/numbers/& actual inputs but none of it is working. Could you explain it a bit further please?

Sorry once again, I know I'm pretty slow at catching on to this stuff. Bear with me please :slight_smile:


#16

because for even lists, len / 2 gives the right middle value. so len / 2 -1 gives left middle value. again, you can do the math for this:

2 / 2 = 1
4 / 2 = 2
6 / 2 = 3

and so on, the first number is list length, divided by two, which gives the right index (remember, lists are zero indexed based, so if list has length of 4, the indexes are: 0, 1, 2 and 3. In other words, 2 is the right middle index)

x[0] was just showing how to retrieve the item at index 0 from list x. It was just a demonstration of concept, not something you should actually use. In your case, the list is num and the index is stored in x variable


#17

oh that makes sense then because I kept thinking that it gives the middle left value first so it was the other way around for me.

Is this what you're referring to:

I've tried this before but it gives me this error: Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


#18

wait, i am too hasty, let me rephrase that


#19

what you try is a step closer, but it doesn't work. For even lists, you will first need to retrieve the values from the list, add them together and divide them by two.


#20

wait, wait.. so we're talking about this section of the code:
if len(num) % 2 == 0:
x = (len(num)/2 + len(num)/2 -1) / 2

?