Median


#1



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

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


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


#2

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


#3

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