# Understanding this minmax function

1. Jan 4, 2016

### xDk

Hey guys, so im going through "Learning Python" book and I came across this minmax function.
Code (Python):

def minmax(test, *args):
res = args[0]
for arg in args[1:]:
if test(arg, res):
res = arg
return res
def lessthan(x, y): return x < y
def grtrthan(x, y): return x > y
print(minmax(lessthan, 4, 2, 1, 5, 6, 3))
print(minmax(grtrthan, 4, 2, 1, 5, 6, 3))

I dont understand the if statement "if test(arg, res)" I haven't seen an if statement like that before. So that is the first issue. When I run the first function separately, it spits backs what ever I type in as the second argument. When I run either of the second functions it is a true or false. So how do combining the two functions give the min or max values of the numbers. Any help is much appreciated.

Last edited: Jan 4, 2016
2. Jan 4, 2016

### Staff: Mentor

Please put code tags around your code. This makes the code easier to read, and in the case of Python, preserves the indentation, which is crucial.

If have done this below, with [code=python] at the top, and [/code] at the bottom.
The test parameter in the minmax function is a placeholder for whatever function you pass in when minmax is called. It will return True or False.

Don't run the first function separately -- it is intended to be used as in the two print statements at the bottom of your code. Notice that in the first call to minmax() (in the first print statement), the function being passed is the lessthan function, along with a list of 6 numbers. In the second call to minmax() (in the second print statement), this time the function being passed is the grtrthan function, together with the same list of 6 numbers.

3. Jan 4, 2016

### xDk

Code tags are fixed :)
Oh I see now, that makes more sense. So the lessthan and grtrthan function are used as the argument test in the minmax function.
So if the lessthan and grtrthan functions give true or false answers, how does it give int answers when run together? Is it correct to say that when for example the lessthan function being run as test in the minmax, when x and y are both 1 at some point in the for loop, that is the only time it wont be true (1 as x, being less than y, will always give true answers for every number but its self) so it gives that number in the print?

4. Jan 4, 2016

### Staff: Mentor

The int value is what is returned by minmax().
That won't happen with the data you show, as neither list of numbers contains duplicates.
All that lessthan does is return the smaller of x and y. If x happens to be equal to y (which, again, doesn't happen in the data you show), lessthan returns False. The only situation in which lessthan returns True is when x < y. If x $\ge$ y, lessthan returns False. The situation is similar for the other comparison function.

5. Jan 4, 2016

### Staff: Mentor

What minmax() does is to look at args[0], and then compare it to each of the other elements of the list, using whichever comparison is passed when minmax() is called. If lessthan is passed, minmax() returns the smallest value in the list. If grtrthan is passed, minmax() returns the largest element of the list.

6. Jan 4, 2016

### xDk

Ah okay, makes sense now, thanks mate :D
Just curious, if I have future questions like this, is it better to post in the homework section or is this fine?

7. Jan 4, 2016

### Staff: Mentor

If you have a question about how to write code, it should go in the HW section. More general questions, like the one in this thread, are OK here.

8. Jan 14, 2016

### ChrisVer

This is a general thing and not only for python. If statements check for boolean expressions.
In other words your test(arg,res) is a boolean*, which can be either True or False, and the "if True" accesses the statements in if, while "if False" ignores them.

*Here your test function that is the lessthan or grtrthan return a boolean: x<y or x>y (that is either True/False).

The following codes do the same thing:
Code (Python):

def myfun1(x,y):
result=0
if(x>y):
result=1
return result
print myfun1(2,3)
print myfun1(3,2)

Code (Python):

def gt(x,y):
return x>y

def myfun2(test,x,y):
result=0
if test(x,y):
result=1
return result

print myfun2(gt,2,3)
print myfun2(gt,3,2)