Median. Would you help me here?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


Getting this error: unsupported operand type(s) for %: 'list' and 'int' ... what it means ?


def median(numbers):
    numbers = sorted(numbers)
    if numbers % 2 == 1:
        first_middle = (len(numbers) // 2.0 - 1)
        second_middle = (len(numbers) // 2.0 + 1)
        average = (first_middle + second_middle) // 2.0
        return average
        
    else:
        middle = len.numbers // 2.0
        return middle
        
median([6,9,4,1,2,3,1,6,7,4,4,4,1])


#2

Do you see the oxymoron here?

On the point of arithmetic, consider the following:

a / n + 1
a / n - 1

These are going to be separated by 2, and not be side by side.


#3

Implying (a / n) + 1 and - 1 ?


#4
6 / 2 - 1 == 2

6 / 2 + 1 == 4

Did you spot the oxymoron in the earlier example?


#5

Sorry no ..... what's wrong here ?


#6

Python 2 does automatic floor division when dividing an integer by an integer.

n / 2 => type int

Writing,

n // 2.0

is fudging. 2.0 is not a counting number, 2 is. We should not massage counting numbers to floats. It is a poor practice to make a habit. Avoid it.

float(n) / 2

is explicit and doesn't bastardize the counting number.

Consider, when counting do we say,

1 point oh
2 point oh
3 point oh

No. We count, 1, 2, 3, ...


#7

I know that lol ....

2.0 wasn't my idea. It seemed odd to me as well but I read this:

Last but not least, note that (2 + 3) / 2 is not the same as (2 + 3) / 2.0! The former is integer division, meaning Python will try to give you an integer back. You'll want a float, so something like (2 + 3) / 2.0 is the way to go.

So I gave it a go .....


#8

def median(numbers):
numbers = sorted(numbers)
if numbers % 2 == 1:
first_middle = (len(numbers) / 2 - 1)
second_middle = (len(numbers) / 2 + 1)
average = (first_middle + second_middle) / 2
return average

else:
    middle = len.numbers / 2
    return middle

median([6,9,4,1,2,3,1,6,7,4,4,4,1])


#9

Traceback (most recent call last):
  File "python", line 13, in <module>
  File "python", line 3, in median
TypeError: unsupported operand type(s) for %: 'list' and 'int'

Line 3 attempts to take a modulo of a list. I suspect you meant to write,

if len(numbers) % 2:

Note that I left off the == 1. A non-zero quantity will evaluate to True in a conditional expression. Either form is okay, but I prefer the truthy version above.

Still testing...

New error

median([1]) returned 0 instead of 1

Consider your list...

a = [6,9,4,1,2,3,1,6,7,4,4,4,1]
a.sort()
print a

# [1, 1, 1, 2, 3, 4, 4, 4, 4, 6, 6, 7, 9]
#                    ^
#                  median

Your code returns 6.

        first_middle = (len(numbers) / 2 - 1)
        print first_middle                    # 5
        second_middle = (len(numbers) / 2 + 1)
        print second_middle                   # 7

See the difference between these two? 2. They need to be adjacent (difference of 1).

Still testing...

Another error.

Traceback (most recent call last):
File "python", line 16, in
File "python", line 15, in median
AttributeError: 'builtin_function_or_method' object has no attribute 'numbers'

middle = len.numbers / 2

I'm certain you meant to write,

middle = len(numbers) / 2

Still getting this error...

median([1]) returned 0 instead of 1

The line,

return middle

is returning the length divided by 2 rather than the value at that index.

return numbers[middle]

Now we get this error,

median([4, 5, 5, 4]) returned 5 instead of 4.5

This boils down to two minor errors in the code.

  1. The odd length case is returning the even length median
  2. The median being returned is the average of the indices, not the values at those indices.

I've simplified the code for clarity, and now have all the issues ironed out...

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

print median([6,9,4,1,2,3,1,6,7,4,4,4,1])   # 4

print median([6, 8, 12, 2, 23])             # 8

#10

This is my most recent code:


#11

You've been around long enough to know how formatted code looks. Please post it. Screenshots are not the way to present code; only results present that way.


#12

Your code works wonderfully. My code didn't work mainly 'coz I was returning average of the indices not the values behind them.

Thank you !


#13

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