15. median


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#

Hi! so I've been getting all kinds of errors with this one. At first my sorted(lst) wasn't working properly. I'm not sure if thats fixed or not because as i was trying new things to get it to work a new error appeared.

File "python", line 7
    else:
       ^
SyntaxError: invalid syntax

This makes no sense to me since i started getting this error when i changed my code back to my original code which seemed to work, with the exception of the sorting.
Any help is appreciated :slight_smile: thanks!


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


#2

i think the function sorted does not change original list , so to change it (sort original list ) stock the output to ( another or original ) list


#3

also you miss one bracket ")" in return (lst[len(lst)/2] + lst[(len(lst)/2) - 1] / 2.0


#4

lol! I totally didn't see that bracket! Thanks! Also I sorted my list into a new list, and used that for calculations which worked! thank you very much!

Here's my finished code for anyone interested.

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

#5

Addendum

When we don't need to preserve the original list, we can sort it in place with,

lst.sort()

The proviso of course is that a list object is not copied when passed to a function. The global object remains in scope, even within the function. Any changes made to the list are reflected in that object.

When we create a sorted copy, the original remains in tact so the global object is not affected by any changes to the copy.


#6
def median(l):
    nl = []
    nl = sorted(l)    
    count = len(nl) / 2 #count of elements from the middle    
    if len(nl) % 2 == 0: #check for even
        med = ((nl[count- 1]) + nl[count]) / 2.0
    elif len(nl) == 1:
        med = nl[0]
    else: 
        med = nl[count]
        
    return med    
    

print median([6,8,12,2,23])

#7

If we are not destroying data, it should not matter that we sort the original list in place.Combine this with what I derive from your code and we get this...

def median(n):
    n.sort()
    c = len(n)
    d = c / 2
    if c % 2 == 0:
        return ( n[d-1] + n[d] ) / 2.0
    elif c == 1:
        return n[0]
    else:
        return n[d]

(untested)

Tested...

def median(n):
    if isinstance(n, list) and len(n):
        n.sort() 
    else:
        return False
    # confirmed that it is a list with length
    # still assumed to be a list of numbers
    c = len(n)
    d = c / 2
    if c % 2 == 0:
        return ( n[d-1] + n[d] ) / 2.0
    elif c == 1:
        return n[0]
    else:
        return n[d]