Median


#1



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


Oops, try again. median([1]) returned [1] instead of 1
wth?


i dunno wuts wrong


def median(lst):
    fds = sorted([lst])
    length = int(len(fds))
    thing = fds[0]
    if length == 1:
        return thing
    elif length % 2 == 1:
        length /= 2 
        item = fds.index(length)
        return item
    else:
        length /= 2
        obj1 = fds.index(length)
        obj2 = fds.index(length - 1)
        item = obj1 + obj2 / 2 
        return item


#2

Point1.

input is already a list, why you're putting it again in brackets?

Point2.
length = int(len(fds))

len() returns the integer why youre casting it again?

Point3.
also you have used the .index() method, when we apply it on list , list.index(item), we pass item and if it exist ,It returns the first occurrence index of that item.
why not just use bracket notation to access items , fds[length] ?

point4.
in final else clause,

item = obj1 + obj2 / 2
You need to make few changes.
due to operator precedence you will have wrong result, even you fix this using brackets you will have error due nature of operands (one should be float to have float result ).


#3

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