Understanding this minmax function

  • Thread starter Thread starter xDk
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around understanding a custom Python function called minmax, which is designed to return either the minimum or maximum value from a list of numbers based on a comparison function passed as an argument. Participants explore the mechanics of the function, specifically the use of an if statement that evaluates a boolean expression, and how it interacts with the provided comparison functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the if statement "if test(arg, res)" and its functionality within the minmax function.
  • Another participant explains that the test parameter in minmax serves as a placeholder for any comparison function, which returns a boolean value.
  • There is a discussion about the intended use of the minmax function, emphasizing that it should not be run separately but rather in conjunction with the print statements provided.
  • Participants clarify that the minmax function compares the first argument against the rest using the provided comparison function, returning either the smallest or largest value based on the comparison.
  • One participant questions how the boolean outputs from the comparison functions lead to integer results when minmax is executed.
  • Another participant explains that the comparison functions return True or False based on the relationship between the two numbers being compared.
  • There is a mention that the minmax function will not encounter equal values in the provided data set, which could affect the behavior of the comparison functions.
  • A participant seeks advice on whether future coding questions should be posted in a specific homework section or if the current forum is appropriate.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the minmax function and the role of the comparison functions. However, there are nuances in understanding the behavior of the if statement and the implications of equal values in comparisons that remain somewhat unresolved.

Contextual Notes

Some participants note that the behavior of the comparison functions may vary depending on the input values, particularly regarding the handling of equal values, which is not present in the current examples.

Who May Find This Useful

This discussion may be useful for individuals learning Python, particularly those interested in understanding higher-order functions and boolean logic in programming.

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 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 50 ·
2
Replies
50
Views
6K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K