Understanding this minmax function

  • Thread starter xDk
  • Start date
  • Tags
    Function
In summary, the lessthan and grtrthan functions are used as the argument test in the minmax function. When the lessthan and grtrthan functions give true or false answers, it gives int answers.
  • #1
xDk
8
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
  • #2
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.
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.
 
  • #3
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 [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.
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?
 
  • #4
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.
 
  • #5
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
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?
 
  • #7
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.
 
  • #8
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)
 

1. What is a minmax function?

A minmax function is a mathematical function that returns the minimum or maximum value of a set of numbers or variables. It is commonly used in computer science and game theory to determine optimal strategies or to find the best possible outcome.

2. How does a minmax function work?

A minmax function works by evaluating each element of a set and comparing them to find the smallest or largest value. This process is repeated until the entire set has been evaluated and the minimum or maximum value is determined.

3. What is the purpose of using a minmax function?

The purpose of using a minmax function is to find the best possible outcome or strategy in a given scenario. It is commonly used in decision-making processes, game theory, and optimization problems.

4. Can a minmax function be used for any type of data?

Yes, a minmax function can be used for any type of data as long as it can be compared and evaluated. This includes numbers, strings, and boolean values.

5. Are there any limitations to using a minmax function?

One limitation of using a minmax function is that it can only determine the best possible outcome based on the data provided. It does not take into account other external factors or variables that may affect the outcome. Additionally, it may not always provide the most efficient or optimal solution, as it only considers the extremes of the data set.

Similar threads

  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
4
Views
918
  • Programming and Computer Science
Replies
8
Views
797
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
825
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
653
  • Programming and Computer Science
Replies
1
Views
690
Back
Top