Median


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5

Error: Oops, try again. median([1]) resulted in an error: list indices must be integers, not float.

Can anybody explain me pls, what I ve done wrong? Cause I dont have a clue.

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

#2

When a list contains only one element, the median is the value at index[0]. Check the length of the list first, and return that value if the length is 1.

The D.R.Y. approach would be to store this value in a variable so the function doesn't have to be repeated four times.

    n = len(numbers)
    if n < 1:
        return "Empty list error"
    if n < 2:
        return numbers[0]

Now proceed using n everywhere you have len(numbers). Let us examine your code once the changes are made and we'll see if there are any more issues.


#3

The heck you need that for?

Just simply telling it you want it as an int forces it to truncate the following digits.[

Should look more like

def median(numbers):
    numbers = sorted(numbers)
    median = 0

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

    return median

Remember explicit is better than implicit, meaning you should explicitly convert to the type you need.


#4

I need round(len(numbers) / 2) to find out the middle index of the list. What would be your solution?

I edited the code as you said. Now it looks like this:

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

however now it says: Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5.
I dont really get this.


#5

When a number is rational, that is, an int / int division, Python returns the floor value in the quotient. This line need only read,

median = numbers[n/2]

Eg.

print 7 / 2     # 3

To return a float, divide by a float, 2.0 and your result will be 4.5 as expected in the SCT test.

    median = (numbers[n / 2] + numbers[n / 2 - 1]) / 2.0

#6

Oh, now I get it.

So in the first case i want to divide by 2.0 to get a result that is a float. On the other hand in the second case, I want to divide by 2, because I need the result to be floored so that I can achieve the position of the middle index in the list.

Nice! Thank you.


#7

We still need to bear in mind what @zeziba advised about 'implicit' versus 'explicit'. What we have done is implicit. Perhaps okay for this situation but in a longer program we are always better off to not leave any little mysteries floating about.


#10