I'm Having Trouble With Median


#1



Stuck at meduan. I'd appreciate any help


median([1]) resulted in an error: unsupported operand type(s) for /: 'list' and 'float'


Replace this line with your code. 

def median(listt):
    sorted(listt)
    if len(listt) % 2 == 0:
        first_index = len(listt) / 2 
        second_index = (first_index - 1)
        answer = float(listt[first_index]) / float(listt[second_index])
    else:
        answer = listt[len(listt / 2)]
    return answer


#2

Needs to be assigned as this is not an in-place method.

This will not give a median of an even length list. Your are dividing the two neighbouring elements, one by the other. It should be the sum of the two divided by 2.


#3

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