Problem with median


#1

Hi I am facing a problem with media as I can't get the median when the list has an odd length so please help

def median(numbers):
    numbers.sort
    length = len(numbers)
    if length % 2 == 0:
        r = length / 2
        answer = (r + 1) / 2
        return answer
    elif lenght % 2 != 0 :

#2

here:

numbers.sort

sort() is a method call, where are the parentheses?

here:

elif lenght % 2 != 0:

what are you going to do when list has uneven/odd length?


#3

Thanks a lot. Actually I want you to tell me what to do after the elif.


#4

you need to get the middle index and then retrieve the middle value using the middle index and return it

as for even lists, lets look at your code:

        r = length / 2
        answer = (r + 1) / 2

r is only a list index, not a list value. How can we get from list indexes to list values?


#5

Please a bit of more help about how to do that.Thanks.


#6

lets say we have this list:

[4,4,5,5]

the indexes are: 0, 1, 2, 3. We can use the length to calculate the middle two indexes (1 and 2) using the length of list, how do we get from index 1 and 2 to the values (4 and 5)?

Accessing list by index is something you should know, if not, find in docs/earlier exercise


#7

I think like this print list_name[index number]


#8

it is indeed, now you will need to implement this into your median program given the median value are list values not list indexes


#9

Alright now this is another try that has failed I think I did what we discussed so please help and thanks.


#10

the function should return numbers, not ilst. So why you would do lst.append() and not just return answer is a mystery to me.


#11

Returning the answer directly might work with the odd case but with the other it won't I have modified it but still not working.

def median(numbers):
    lst = []
    numbers.sort()
    length = len(numbers)
    if length % 2 == 0:
        r = length / 2
        new = numbers[r]
        for n in range(new,new+2):
            total += n
            new_tot = total / 2
        lst.append(new_tot)
    
        
    elif length % 2 != 0 :
        n = length / 2
        answer = numbers[n]
        #lst.append(answer)
    return answer

#12

for even lists you obviously should return new_tot

I told you what is expected of your function, and one example. You still have to do the thinking bit, oh, i need to return integer, so i will also need to change my part for even lists.

We are here to help you, not provide you with the solution.

Helping only works if you keep thinking :wink:


#14

A post was split to a new topic: Median


#15

Alright another edit and still not working.

def median(numbers):
    lst = []
    numbers.sort()
    answer = 0
    length = len(numbers)
    if length % 2 == 0:
        r = length / 2
        value = numbers[r]
        nr = r + 1
        val = numbers[nr]
        answ = (value + val) / 2
        return answ
    
        
        
        
    elif length % 2 != 0 :
        n = length / 2
        answer = numbers[n]
        #lst.append(answer)
    return answer

#16

here:

        r = length / 2
        value = numbers[r]
        nr = r + 1

if the list has a length of four (indexes are: 0, 1, 2, 3) according to your code the middle two indexes are index 2 (r) and index 3 (nr). Please explain

here:

answ = (value + val) / 2

if the division involves only integers, python will give you a integer as result (if needs be, the number is rounded down to the nearest integer), but you want a float, so the division should involve a float


#17

What is a float ? What does it mean ?


#18

You are kidding right? At this point in the course i assume you are familiar with basic types (integer, boolean, float, string, list, dictionary) in python


#19

I am sorry but I am not kidding is there another exercise that explains floating ?


#20

Plenty of exercises you have done so far, and i am sure you can find documentation on float data type with a little googling

Its impressive in a certain way you got this far in the course without understanding fundamental things about python


#21

The exercise said that if the length is an even the we get the sum of the 2 indexes in the middle and divide it by 2
and this is the median and this what I wanted to do but it seems that I failed
by the way I think I have remembered what float is and what it does. But I still fail to use or even see how it is useful in here.