Understanding this minmax function

  • Thread starter Thread starter xDk
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
The discussion revolves around understanding the `minmax` function in Python, which takes a comparison function and a variable number of arguments to return either the minimum or maximum value. The key point is that the `test` parameter serves as a placeholder for the comparison function (like `lessthan` or `grtrthan`), which returns a boolean indicating the relationship between two numbers. When `minmax` is called, it compares each number in the list using the provided function, ultimately returning the smallest or largest value based on the comparison. The confusion about the `if test(arg, res)` statement is clarified, emphasizing that it evaluates to True or False, guiding the selection of the minimum or maximum value. The thread concludes with a suggestion that general coding questions are appropriate for the current forum section, while specific homework-related inquiries should be posted elsewhere.
xDk
Messages
8
Reaction score
0
Hey guys, so I am going through "Learning Python" book and I came across this minmax function.
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 don't 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:
Technology news on Phys.org
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
Python:
 at the top, and
at the bottom.
xDk said:
Hey guys, so I am going through "Learning Python" book and I came across this minmax function.
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 don't 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.
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.
 
Mark44 said:
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
Python:
 at the top, and
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.
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 won't 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?
 
xDk said:
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?
The int value is what is returned by minmax().
xDk said:
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 won't happen with the data you show, as neither list of numbers contains duplicates.
xDk said:
, that is the only time it won't 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?
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.
 
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.
 
Mark44 said:
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.
Mark44 said:
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.
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?
 
xDk said:
Just curious, if I have future questions like this, is it better to post in the homework section or is this fine?
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.
 
xDk said:
I don't understand the if statement "if test(arg, res)"

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:
Python:
def myfun1(x,y):
    result=0
    if(x>y):
        result=1
    return result
print myfun1(2,3)
print myfun1(3,2)

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)
 

Similar threads

Replies
18
Views
1K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
50
Views
5K
Replies
16
Views
2K
Replies
3
Views
1K
Replies
8
Views
1K
Replies
4
Views
2K
Replies
2
Views
956
Back
Top