Exercise 15/15 for Practice makes perfect


#1

Exercise 15/15 for Practice makes perfect.


Error: Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5

I don't understand why it keeps saying this, when it should have been sorted by this point already.

def median(moot):
    size = len(moot)
    m = sorted(moot)
    
    if size % 2 == 0:
        half = size / 2
        average = (m[half] + m[half+1]) / 2.0
        return average 
    elif size == 1:
        return m[0]
    else:
        medin = m[((size / 2) + 1)]
        return medin

#2

Instead of adding 1, subtract 1. Using an implicit float is less preferred than explicitly declaring it. Counting numbers should be left as integers.

average = float(m[half - 1] + m[half]) / 2

#3

Yeah, I literally JUST figured this out, before checking my thread.

"Thank you Archived QA section!" <-- Seriously, check it, it works ad makes you notice things. Wait a minute, you're a moderator....NVM.


#4

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