15. median - Please help!


#1




When I run my code, i get the error message:

Oops, try again. median([1]) returned 0 instead of 1


it should take a list of numbers and sort them and then return the middle number (or if the set of numbers is even, return the middle two)

I am very new to python, and all help is appreciated! :slight_smile:

def median(x):
    s = sorted(x)
    l = len(x)
    r = 0
    for i in s:
        if l % 2 == 0:
            r = l / 2 + l / 2 - 1 / 2.0
        else:
            r = l / 2
    return r
print median([1,2,3,4,4])


#2

So you're pretty close. Your issue is that you need to have r relate to the indices in s, because right now you're just returning numbers and not relating back to the original list at all. For example, your else statement should be:
else:
r = s[l/2]

Hopefully that helps, I got your code to work with just a couple small adjustments so you're on the right track.


#3

Thank you so much! I've been looking around the forums for this one a little and then doing my code. This one is definitely a challenge! :slight_smile:


#4

I changed a few things, and know I am getting 3.5 instead of 4.5.

def median(x):
s = sorted(x)
l = len(x)
r = 0
for i in s:
if l % 2 == 0:
r = l / 2 + l / 2 - 1 / 2.0
else:
r = s [l / 2]
return r
print median([4,5,5,4])


#5

So you will still have to modify the if statement as well as the else. Again, it's only spitting out a number, not an index in s. Can you think through how you would modify your if statement to make the code work?


#6

It has to be the length of the sorted list, not the original list maybe? If so would i have to change what the l stores here?:

l = len(x)


#7

Not quite, the length of the list isn't changing, just the order. What do we want the function to do? We want it to check if the length of the list is even. If it is, we want it to take the values from the two indices in the middle and average them. Otherwise, just print the middle index.

Right now your code is taking the integer from the length of the list and manipulating it, regardless of what is actually in the list. So any list with 4 indices will result in 3.5 (4/2 + 4/2 - 1/2), and any list with 8 indices will result in 7.5 (8/2 + 8/2 - 1/2). Check your order of operations as well, some clarifying parentheses may help. I can post your corrected code if you need it, but you're so close I think you can get it from here.


#8

I changed my code and now it is returning the right answer (Hooray!), but I am getting the error message:

Oops, try again. median([1, 34, 1, 6, 8, 0]) returned 5.5 instead of 3.5

Here's my code:

def median(x):
    s = sorted(x)
    l = len(x)
    r = 0
    for i in s:
        if l % 2 == 0:
            r = (s[l / 2] + s[l / 2] - 1) / 2.0
        else:
            r = (s[l / 2])
    return r
print median([4,5,4,5])

I'm so close! Thanks for all of your help so far. I really appreciate the people who help on the forums. :slight_smile:


#9

I meant it returns the right answer when i call median on [4,5,4,5] but it doesn't work if you put in [1, 34, 1, 6, 8, 0]


#10

Try this:

def median(x):
    s = sorted(x)
    l = len(x)
    r = 0
    for i in s:
        if l % 2 == 0:
            r = (s[l / 2] + s[l / 2 - 1]) / 2.0    #correction s[l/2 - 1] instead of s[l/2] - 1
        else:
            r = (s[l / 2])
    return r
print median([4,5,4,5])

[4, 5, 4, 5], after sorting becomes [4, 4, 5, 5]. Here, list length, l = 4. You need middle two digits, i.e. 4 (index = 1) and 5 (index = 2). Now, s[l/2] = s[2] = 5, But, s[l/2] - 1 = s[2] - 1 = 5 - 1 = 4, which is correct for this particular case. Actually you need the middle index that corresponds to 4, i.e. index = 1. You can have by s[l/2 - 1] = s[2-1] = s[1] = 4.


#11

Oh my goodness! I should have tried that! Thank you so very much @meason and @neonhights for helping me solve this! It finally works! :smile: :smile:


#14

Hi! That's my code

In my PyCharm it work's well, but here it print's "Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5", and at the same time in my Pycharm it print's 4.5
So, where is my error?


#15

Hi @microsurfur45418,

I'm afraid I can't really say /why/ it works, but I suspect it has something to do with the number being turned into an int, which lacks decimal values, rather than a float.

Try this under your first 'if':

res = (nums[int(len(nums) / 2) -1] + nums[int(len(nums) / 2)]) / 2.0

By dividing it by 2.0 instead of just 2 the problem is solved.

Perhaps someone more experienced than me can shed some light on why this is the case?


#16

O, thank you, I think so too, and I have already solved this problem)


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.