0

I am right now a Python newcomer, and I study this by finishing courses on http://www.codecademy.com. This website offers an online judge system just like an ACM to exam if my code works fine. And it works great on my others code except this one.

I tried to copy this code to my local pc with Python 2.6 installed and it works.

By the way, can you suggest some Python grammar books to a beginner like me, I just want to know a little inner detail about this language...

Because I cannot post a picture here I just paste the code below:

on my mac: :

    [~ tk$]cat test8.py 
def median(li):

    if len(li) >= 2:
        li_test = sorted(li)
        if len(li_test)%2 == 0:
            cd1 = len(li_test)/2-1
            cd2 = cd1 
            re = (li_test[cd1] + li_test[cd2])/2.0
        else:
            cd = (len(li_test)+1)/2-1
            re = li_test[cd]
    else:
        re = li
    return re
print median([1,2,3])

[~ tk$]python test8.py 

2

[~ tk$]

on the websites: the title is : Practice makes perfect 15/15:

def median(li):

    if len(li) >= 2:
        li_test = sorted(li)
        if len(li_test)%2 == 0:
            cd1 = len(li_test)/2-1
            cd2 = cd1 
            re = (li_test[cd1] + li_test[cd2])/2.0
        else:
            cd = (len(li_test)+1)/2-1
            re = li_test[cd]
    else:
        re = li
    return re


Oops, try again. Your function crashed on [1] as input because your function throws a "float() argument must be a string or a number" error.

1 Answer 1

1
else:
    re = li

The error is occurring because if the length of li is 1 or 0, then you are returning the list li.

>>> median([1])
[1]
>>> median([0])
[0]

Maybe you want

if len(li) >= 1:
  ...
else:
    raise IndexError("No items in this list")

Another bug in your code is if there are even number of elements in the list, you should take the average of the two middle elements. But in your code,

    if len(li_test)%2 == 0:
        cd1 = len(li_test)/2-1
        cd2 = cd1 
        re = (li_test[cd1] + li_test[cd2])/2.0

You take one of the two middle elements, and add the same number twice and divide by two. It should be

    if len(li_test) % 2 == 0:
        cd1 = len(li_test) / 2
        cd2 = cd1 - 1
        re = (li_test[cd1] + li_test[cd2])/2.0
Sign up to request clarification or add additional context in comments.

4 Comments

No, I don't think I can do that. It is a online judge system, So I can only make my codes to satisfy its criterion. In the case thatthe length of [1] is 1, clearly the median number is 1 so I just returned the number. I can't see the mistake.
You are returning the list [1] not the int 1
TK, to illustrate Jay's second point -- what is the median of [1,3]?
Sorry, I am just being busy with my Classes.. I know where my fault is already.[0] and 0 is not the same thing. Sorry I used to be a Matlab user and those two are pretty confusing. It looks I need some more practices. Thanks for your guys help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.