Median challenge?


#1

I'm doing the median challenge and finally written some code that I believe should work:

def median(numbers):
    if len(numbers) % 2 == 0:
        return (numbers[len(numbers)/2]+numbers[len(numbers)/2 - 1]) / 2.0
    else:
        return numbers[(len(numbers)/2)]

However an error comes up saying that the input ([4,5,5,4]) should give a median answer of 4.5 instead of my 5.0. I thought a median was the middle number or the average of the middle numbers in this case (as there are an average number of indices in the list), in which case it should be 5.0 as the average of the two middle numbers (5 and 5) is 5.0 ((5+5)/2 = 5). This would mean that the course creator got it wrong. However I'm thinking surely this doesn't happen.

Am I interpreting the challenge wrong or missing something here?


#2

Well, I'm an absolute idiot. I read over challenge again and realised I had forgot to sort the list. Done now!!


#3

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