Median question: float works, int doesn't!


#1


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

Hi everyone! I've got past the median lesson, but I'm confused as to why. I use the code


def median(blergh):
    x = sorted(blergh)
    median = 0
    if len(x) % 2 != 0:
        median = x[(len(x) - 1) / 2]
    elif len(x) % 2 == 0:
        median = (x[(len(x)/2)] + x[len(x)/2 - 1]) / 2
    return median

then, it doesn't work; I get the error:
Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5

then, I try the code

def median(blergh):
    x = sorted(blergh)
    median = 0
    if len(x) % 2 != 0:
        median = x[(len(x) - 1) / 2]
    elif len(x) % 2 == 0:
        median = (x[(len(x)/2)] + x[len(x)/2 - 1]) / (2.0)
    return median

and this works; all I did was replace the 2 in line 7 with a 2.0. What I suspect is that I can't divide by an integer, and perhaps the len() function works only with a float, for some reason. Is this on the right track? In any case, what I'd like to know is why replacing the 2 with a 2.0 changes the way the median is calculated, and why it ends up working. Thank you so much for your time!


#2

You can divide by an integer, but Python 2 returns an integer when numerator and denominator are both integers. We can use implicit floats, but that changes the complexion and semantics of the expression. I prefer (and readers understand) explicit declaration of a number that may be a float. In this case, it would be the numerator since 2 is a counting number (as is len).

float(a) / 2

#3

Thanks for your reply! I think I understand what you're saying. I still don't understand, however, exactly why it is that my first example fails, but my second example works. Thanks again for your time!


#4

Your first example failed because it did not return a float, as indicated in the error message.


#5

Thanks for the reply. I think I understand now. Have a great day!


#6

Something of note that I meant to mention earlier, median = 0 is unnecessary. It does not need to be initialized since it is defined at each instance in the code, not accumulated. What could be of benefit to the reader, and the program in general is caching the length rather than calling the funciton each time.

n = len(x)

We can also do some of the math right here.

m = int(n / 2)

#7

I see. This increases efficiency and readability as well, right? Thanks for the reply!


#8

A number of things contribute to readability, starting with code that makes sense and reduces repetition so the flow and meaning is more visible. Keeping the number of variables to a minimum also helps, as does giving them meaningful names.

Readability is as much subjective as objective, and in the hands of the author. If the author is not expecting anybody to read their code it doesn't lessen the need for readability... It helps us the most when we come back to the code six months later.


#9

Thanks for the reply. Have a great day!


#10

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