Median


#1

I dont get it, after putting 2.0 instead of 2, also i am getting same error
"Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5"

def median(lst):
    lst=sorted(lst)
    if lst==[1]:
        med=1
    else:
        if len(lst)%2!=0:
            med=lst[(len(lst)-1)/2]
        else:
            med=(lst[len(lst)/2]+lst[(len(lst)/2)+1])/2.0
    return med

please suggest a solution


#2

lets see, [4,5,5,4] is 4 in length (the list get sorted, so it becomes: [4,4,5,5]). the indexes are: 0,1,2,3. so len(lst)/2 is index 2. and we need index 1 and 2. so this:

lst[(len(lst)/2)+1]

shouldn't be plus one. since then you have index 3. you need index 1.


#3

Dhanyawaad !!! Thank you !!!