Median problem


#1



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


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


Return with median number in float form


def median(number_list):
    sorted_list = sorted(number_list)
    temp = 0
    list_length = len(sorted_list)
    if len(sorted_list) % 2 == 0:
        temp = sorted_list[((list_length / 2.0) + ((list_length / 2.0) + 1)) / 2.0]
    elif list_length == 1:
        return list_length
    else:
        temp = sorted_list[(list_length / 2.0) + 1]
    return temp


#2

The error is from where you're dividing list_length, you should be dividing by 2 to get integer index values.


#3

I still get the same error


#4

Is it still the "list indices must be integers, not float" error? Integer division (dividing by 2 instead of 2.0) should resolve that.

Additionally, your code also appears to be dividing by 2 again (after dividing list_length) inside the index instead of averaging the two values at those indexes.


#5

While it may be possible to implicitly define a float by dividing by a float, it is not a recommended practice. Explicit declaration is better.

D.R.Y. means do not repeat yourself. The above code is repeated three times which suggests we could simplify the code by defining a variable with this value cached.

mid = list_length / 2

Length is an integer, and dividing by an integer yields an integer (in Python 2.x). Now consider that lists are zero-indexed, so dividing length by 2 will give a value that is the middle index of an odd length list, and the right hand index of the middle two values in an even length list.

9 / 2 == 4

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

Notice there are four indices on either side?

8 / 2 == 4

[0, 1, 2, 3, 4, 5, 6, 7]
             ^

Notice that there are three indices on the right side, and four on the left side. This means that the left side middle index is 4 minus 1, not plus.

The variabale does not need to be initialized since your code assigns a value which replaces the initial value.

Since you are testing the length directly in the if statement, the above line may be replaced with the mid statement given earlier in this post.

1 is an odd length, so the mid will be index 0. The above line is not needed.

def median(number_list):
    sorted_list = sorted(number_list)
    mid = len(sorted_list) / 2
    if len(sorted_list) % 2 == 0:
        return float( sorted_list[mid - 1] + sorted_list[mid] ) / 2
    else:
        return sorted_list[mid]

Hope this makes sense.


#6

This is great, thanks for the clarifications!


#7

Excuse my confusion. How does this work? Should the mid of a list with an odd number of values not be a float?

e.g. numbers = [1, 3, 5]
len(numbers) == 3

The float then cannot be an index from my understanding


#8

No. The middle index is an integer. The value at that index may be anything.

3 / 2  ==  1

in Python 2.x. The median is 3, given the list, [1, 3, 5] since it is the value at index 1 of the sorted list.

Python 3.x requires explicit declaration of integers, opposite of 2.x, which requires (so to speak) explicit declaration of floats. We can get in the habit of writing more explicitly even while the interpreter is 2.x. That way we can write in the 3.x paradigm using 2.x models.

int( 3 / 2 )

This method call supercedes any that are built into the environment. We explicitly want an integer, plain and simple. Self documentation +1.


#9

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