15/15.median Need some advice


#1


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

Oops, try again.
median([4, 5, 5, 4]) returned 3.5 instead of 4.5


In theory my code is expected to work correctly, but instead it always returns a bad value rather then expected one. Where is the error?


def median(x): 
    if len(x) % 2 != 0:
        m = len(x) / 2
        return x[m - 1]
    elif len(x) % 2 == 0:
        xl = sorted(x)
        l = len(x) / 2
        v = l + 1
        c = xl[l] + xl[v] / 2.0
        return c


#2

Move the return to the left to line up with the if.


#3

 def median(x): 
    if len(x) % 2 != 0:
        m = len(x) / 2
        return x[m - 1]
    elif len(x) % 2 == 0:
        xl = sorted(x)
        l = len(xl) / 2
        v = l + 1 
        c = xl[l] + xl[v] / 2.0
    return c

Gives me an error again.

Look:

Oops, try again.
median([4, 5, 5, 4]) returned 7.5 instead of 4.5


#4

This can be just, return x[m]. If len(x) is odd, then m is the middle index.

The list should be sorted before any checks or returns.

def median(x):
    x.sort()
    if ...

This should be written, v = l - 1. Consider:

[ 1, 2, 3, 4, 5, 6, 7, 8 ]
#          ^  ^
#         [3][4]

8 / 2 == 4

#5

def median(x): 
    x.sort()
    if len(x) == 1:
        return x[0]
    elif len(x) % 2 != 0:
        m = len(x) / 2
        return x[m]
    elif len(x) % 2 == 0:
        l = len(x) / 2
        v = l - 1 
        c = x[l] + x[v] / 2.0   
    return c

Changed a bit but again gives me wrong value.

Oops, try again.
median([4, 5, 5, 4]) returned 7.0 instead of 4.5


#6

Too many cases, there's just odd and even

Operator precedence dictates that division is done before addition

You should thus have x[l-1:l+1] / 2 or (x[l] + x[v]) / 2
(I haven't checked if l if it's what it should be, just matching what you wrote)


#7

Ah, found it. Just changed:

c = x[l] + x[v] / 2.0

to

c = x[l] + x[v]
cc = c / 2.0


#8

Hey you'll want to avoid names like c m v l xl .. they're very difficult to reason about, they mean that you have to do all the reasoning beside the code, but the code is also for us humans, we want to be able to look at it and understand it.

Naming isn't easy, but it helps greatly.

Consider:

length = len(sequence)
if length % 2 == 1:
    middle_index = length / 2
    return sequence[middle_index]
else:
    middle_right = length / 2
    middle_left = middle_right - 1
    return sequence[middle_left:middle_right+1] / 2

Names of things should be by what they represent.

A big difference is that you can look at just one line and still be able to tell if it's correct or not. That's not possible if you can't tell what the names mean! With cryptic names you have to hold it all at once in your head, that's something to avoid. We want to split it up to the point where we can validate each component individually as well as that they are fit together correctly.


#9

Got it. Thx for this advice!


#11

What does the != in line 5 do?


#12

It checks if length of x is not even number and executes then the first nuber from the list x