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

def median(list1):
    index1= 0
    index2= 0
    result =0
    for item in list1:
        if len(list1) %2 == 0:
            index1 = len(list1)/2.0
            index2 = index1 - 1
            result = (index1+index2)/2.0
            return result
            result = (len(list1)+1)/2.0
            return result
        return result


why do you have a loop?

for odd lists, you can simply define the length of the list by two to get middle index

for even lists, this is the length of the list divided by 2 and length of the list divided by 2, minus one


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