Median


#1
def median(input_lst):
    if len(input_lst) == 1:
        return input_lst[0]
    else:
        median_lst = sorted(input_lst)
        if len(input_lst) % 2 == 1:
            return median_lst[len(input_lst) // 2 + 1]
        else:
            return (median_lst[len(input_lst) // 2] + median_lst[len(input_lst) // 2 - 1]) / 2.0

error message:

median([6, 8, 12, 2, 23]) returned 12 instead of 8

cant understand why median_lst = sorted(input_lst) doesnt sort input_lst...


#2

Start by checking if that's actually the case. Print it out.


#3

i found out whats wrong almost right after posting it... index for median_lst should be 1 less
so it should go like return median_lst[len(input_lst) // 2]