1

As a homework assignment I have to write a script which finds the median of 3 given numbers without using a standard sort function of Python.

This is my first week in class and my first programming experience so I find it very difficult to get any further than I am right now.

Here's what I have so far:

def med3(a,b,c):
    list = [a, b, c]
    newlist = []
    if list:
        minimum = list[0]
        for x in list:
            if x < minimum:
                minimum = x
                newlist.append(minimum)
                list.remove(minimum)
            elif x >= minimum:
                newlist.append(x)
                list.remove(x)

    return newlist[1]

This seems to do the trick, but only for the first two entries of the list. The loop doesn't include the third entry.

How can I make the script include all three entries?

Thanks in advance! Sander

4
  • 6
    Too much work. Define what "median" means in the context of 3 numbers without using loops and lists. Commented Sep 7, 2012 at 21:43
  • You have a lot of code there that really isn't needed. Also since this is homework, its for you to figure out, not for us to do it for you... Commented Sep 7, 2012 at 21:44
  • 2
    Welcome to Stack Overflow! You have many unnecessary steps here. For example, if list: is unnecessary: you've just defined the list. But in general you don't need lists or loops. Hint: you can do this using three if statements that each use an or. Commented Sep 7, 2012 at 21:45
  • I would suggest to find out a technique to find the median of 3 given numbers in your head. Write down this technique in English as if giving a recipe to someone else. Finally code this technique using a programming language. (Call your text a pseudocode and you're a real programmer ;-) Commented Sep 7, 2012 at 21:51

4 Answers 4

6

sum([a, b, c]) - min(a, b, c) - max(a, b, c) - no sorting!

Sign up to request clarification or add additional context in comments.

Comments

0

You are modifying the list in-place while looping over it, which has consequences for what elements you see:

>>> numbers = [1,2,3]
>>> for i in numbers:
...     if i == 2: numbers.remove(i)
...     print i
... 
1
2

Note how 3 is never printed; by removing the second entry in the list, we've shortened it by one element and the loop finds the list exhausted early.

Note that you don't need to loop over the items, a few simple comparisons will tell you what item is the median if you think about it for a second. :-)

Comments

0

There are a number of simpler ways to go about this, but as for your approach:

You're modifying list inside of your loop. Don't do that. :)

In your case, you should be removing elements from newlist:

def med3(a,b,c):
    list = [a, b, c]
    newlist = []
    if list:
        minimum = list[0]
        for x in list:
            if x < minimum:
                minimum = x
                newlist.pop()
                newlist.append(minimum)
            elif x >= minimum:
                newlist.append(x)
    return newlist[1]

But as an exercise, you might want to think about a few things:

  • Why are you putting the elements in a list and looping over them? What advantage does this have over comparing a,b,c with simply if statements?

  • Why the if list:?

Comments

0

The fastest way to do it:

def medianFast(a, b, c):
    if a > b:
        if b > c: 
            return b
        elif a > c:
            return c
        else:
            return a
    else:
        if b < c: 
            return b
        elif a > c:
            return a
        else:
            return c

Guarantees you 3 comparisons at the worst case and 2 comparisons in the best case. 2,5 comparisons in average.

Using ternary conditional we can write it shorter as:

def medianTernary(a, b, c):
    return (b if b > c else (c if a > c else a)) if a > b else (b if b < c else (a if a > c else c))

If you could use sorting you would have the shortest version:

def medianSorted(a, b, c):
    return sorted([a, b, c])[1]

Comments

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.