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

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
        length /= 2
        obj1 = fds.index(length)
        obj2 = fds.index(length - 1)
        item = obj1 + obj2 / 2 
        return item



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

length = int(len(fds))

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

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] ?

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 ).


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