Median


#1

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

def median(listt):
    lisst = sorted(listt)
    length = len(listt)
    if(length % 2 != 0):
        mid = length / 2
        med = listt[mid]
    else:
        mid1 = length / 2
        mid2 = length / 2 - 1
        med = (listt[mid1] + listt[mid2]) / 2.0
    return med
    
print median([4,5,5,4])

what is wrong


#2

you sort the list, but here:

med = (listt[mid1] + listt[mid2]) / 2.0

you don't use the sorted list


#3

but sir I did this already ...
lisst = sorted(listt)


#4

i told you that you sorted the list. but then here:

med = (listt[mid1] + listt[mid2]) / 2.0

listt is not the variable which contains the sorted list


#5

thanks sir. its done.. thanks for the instant reply.. I thought I will have to wait for days..


#6

You're welcome :slight_smile:

If you make a good question the response time is quit okay.


#7