Median


#1



I'm stuck on Median, can't figure out what's happening.


Error is: "median([1, 34, 1, 6, 8, 0]) returned 17.5 instead of 3.5"


def median(nums):
    sorted(nums)
    if len(nums) % 2 == 0:
        x = len(nums)/2 - 1 
        median = (nums[x] + nums[x-1])/2.0
        return median
    elif len(nums) == 1:
        return nums[0]
    else:
        return nums[len(nums)/2 - 1]


#2

you sort the list, but then don't store the sorted list in a variable, so the sort action has no lasting effect


#3

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