15.median - help please


I need help with my code please!


I keep getting this error -
Oops, try again. median([1]) returned [1] instead of 1

Replace this line with your code. 
def median(stuff):
    for things in stuff:  
        num = len(stuff)
        if num % 2 != 0:
            while len(stuff) > 1:
                stuff -= min(things)
                stuff -= max(things)
            return stuff
        elif num % 2 == 0:
            while len(stuff) > 2:
                stuff -= min(things)
                stuff -= max(things)
                return stuff
                average = (things[1] + things[0]) / 2.0
            return average


This is not an in-place sort. It needs to be assigned to a variable. To do a self-state change, use the method,



temp = sorted(stuff)

The for loop is unnecessary. The while loops would do the job if the for loop wasn't preventing then from working. We cannot mutate the iterable in a for..in loop.

There is a much simpler way to approach the problem than brute force destruction of a list. Not that this way isn't somewhat novel. We had one such procedure pass this way a while back.


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