Median - I understand the problem but


#1

I have the same issue. I understand the problem but i am having difficulty finding a solution.
Preformatted text

def median(lst):
    sorted(lst)
    if (len(lst) % 2) != 0:
        middle = int((((len(lst) + 1) / 2) + 1))
        return lst[middle]
    else :
        middle1 == int(((len(lst) / 2) + 1))
        middle2 == int((((len(lst) + 2) / 2) + 1))
        total == (lst[middle1] + lst[middle2]) / 2
    return total

15. Median
#2

What stops you?
How would you compute the median manually?
Is your code doing those same steps? Find out where it differs


#3

This is for when the list length is odd. Lets say it is length 11...

 int((((11 + 1) / 2) + 1))

This would put the median at index 7. Does that seem right?

With the brackets reduced to only what would be required this is what would still return the same thing:

int((11 + 1) / 2 + 1)  =>  7

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  (these are indexes)
                     ^
                   median

Hint: median = int( 11 / 2 )


#5

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