0

I am looking for the answer to the same question found here:

Python - Find the greatest number in a set of numbers

However, I would like to build my own program instead of using the built-in function max(). This is what I have right now, but it's throwing an error, I think because of variable scope.

def two_of_three(a, b, c):
    if a>=b:
        x=a
    else:
        x=b
    if b>=c:
        y=b 
    else:
        y=c
    if a>=c:
        x=a
    else:
        x=c
    return x**x+y**y

assert two_of_three(3,4,5)==41
assert two_of_three(0,1,2)==5
assert two_of_three(9,21,89)==8362

Here is the error I am getting:

Traceback (most recent call last):
File "python_hw1.py", line 32, in <module>
assert two_of_three(3,4,5)==41
AssertionError
Community
  • 1
  • 1
bmacri
  • 9
  • 1
  • 1
  • 4
  • What error are you receiving? – chroipahtz Feb 26 '12 at 05:43
  • 3
    If it's throwing an error, **tell us what the error is**. By definition, if you don't know what it means, you're not qualified to decide which part is unimportant. – Greg Hewgill Feb 26 '12 at 05:43
  • 3
    I can't find any errors. Also, why are you raising `x` to the `xth` power at the end? That's a somewhat misleading output to your function... – Blender Feb 26 '12 at 05:44
  • what's the purpose of `two_of_three`? – kev Feb 26 '12 at 05:45
  • 1
    Other than the strangeness of this function I dont see a scope issue – jdi Feb 26 '12 at 05:48
  • @GregHewgill - sorry about that, I am new to this. I'm editing my question to include my full program, and a copy of the error I'm receiving. Thanks. – bmacri Feb 26 '12 at 05:49
  • @blender - thanks, I meant to square. – bmacri Feb 26 '12 at 05:57
  • @kev - the purpose is to select the largest two of three numbers, and then return the square of those two larger numbers – bmacri Feb 26 '12 at 05:58
  • @jdi why is this function strange - because of what it's trying to do, or because of the way I wrote it? Just trying to learn :) – bmacri Feb 26 '12 at 05:59
  • Strange because of the naming vs what itbwas doing randomly at the end. And also because we had very little info to help you until you updated. Your method is flawd for the last one because c gets assigned to both x and y – jdi Feb 26 '12 at 06:02
  • I updated my answer to also suggest another method you can try. Some people here have had some very creative solutions. – jdi Feb 26 '12 at 07:13
  • If you want to find the two largest of three numbers without using `max`, simply use `min` to find the smallest, and then the two largest are the other two. :) – Karl Knechtel Feb 26 '12 at 08:32

7 Answers7

3

Sol.1

def two_biggest(a, b, c):
    if a>=b>=c:
        print a, b, 'are the biggest two'
    elif b>=c>=a:
        print b, c, 'are the biggest two'
    else:
        print c, a, 'are the biggest two'

Sol.2

def two_biggest(a, b, c):
    nums = set([a, b, c])
    smallest = min(nums) # not max (trollface :P)
    nums.remove(smallest)
    print "the two largest numbers are", ' and '.join(map(str, nums))
RanRag
  • 48,359
  • 38
  • 114
  • 167
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
2

In Python, ** means to the power of. You are probably looking for return x**2 + y**2

Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thanks, Blender. I changed the code as you said, and ran it, and got this error... >>> assert two_of_three(3,4,5)==41 Traceback (most recent call last): File "", line 1, in AssertionError >>> assert two_of_three(0,1,2)==5 Traceback (most recent call last): File "", line 1, in AssertionError >>> assert two_of_three(9,21,89)==8362 Traceback (most recent call last): File "", line 1, in AssertionError >>> – bmacri Feb 26 '12 at 06:05
  • Don't use `assert`. See what the difference is. – Blender Feb 26 '12 at 06:14
  • @inspectorG4dget's answer is the best solution so far. I suggest you try it, as your code isn't working. – Blender Feb 26 '12 at 06:16
1
l=[2,3,8,5,4]
l.sort()
l[-1]
8
ferdy
  • 7,366
  • 3
  • 35
  • 46
1

You have a bug in your code that allows the same value to be returned twice:

def two_of_three(a, b, c):
    if a>=b:
        x=a
    else:
        x=b
    if b>=c:
        y=b 
    else:
        y=c
    if a>=c:
        x=a
    else:
        x=c
    print x, y

>>> two_of_three(3,4,5)
#5 5

update

i havent tested this because I am updating from my phone, but what about something like this?

vals = [3,4,5]
twoLargest = sorted(vals)[-2:]

Let the sort function put the biggest ones naturally at the end and take the last two?

def biggestTwo(*args):
    return sorted(args)[-2:]
jdi
  • 90,542
  • 19
  • 167
  • 203
0

My solution is the following:

def two_of_three(a, b, c):
    """Return x**2 + y**2, where x and y are the two largest of a, b, c."""
    return sum( map( lambda x: x**2, ( (a, b) if (a>=b>=c) else ((b, c) if (b>= c>=a) else (a, c))) ) )
Tom
  • 1
0

You can try using list comprehensions, if what you need are the largest numbers (not the variable names):

In : (a,b,c)
Out: (7, 3, 8)

In : [x for x in (a,b,c) if (x > a) | (x > b) | ( x > c)]
Out: [7, 8]

You will have to tweak this, depending on the result you want when two or more of the numbers are equal (e.g. 7,7,9)

Ari
  • 460
  • 6
  • 13
  • Glad you like it. I tried to make the answer a bit more compact; and I always like using list comprehensions when appropriate (and even sometimes when not). – Ari Feb 26 '12 at 06:28
0

If the issue is avoiding the bultin max function, write your own:

def mymax(l):
    x=float("-inf")
    for i in l:
       if i>x: x=i
    return x 

If you want a list of n of the greatest values in a list, you can write your own one of those too:

def nmax(pl,n=1):
    r,l=[],[]
    for e in pl:
        l.append(e)
    for x in range(0,n):
        max_found=float("-inf")
        for i, v in enumerate(l):
            if v>max_found: 
                max_found=v
                max_index=i

        r.append(max_found)
        del l[max_index]

    return r   

Test it:

>>> import random
>>> rl=random.sample(range(1000),10)     
>>> print rl
[183, 456, 688, 263, 452, 613, 789, 682, 589, 493]        
>>> print nmax(rl,2)
[789, 688]

Or, if you are allowed the built-in sorted you can do this in one line:

>>> sorted(rl)[-2:]
[789, 688]
dawg
  • 98,345
  • 23
  • 131
  • 206
  • You should mention that your first two (and except the one with `sorted`) only work with positive numbers. You can fix by comparing to `-inf` instead of `0` – the wolf Feb 26 '12 at 20:04