Emulating Python's min() and max()
00:00
Emulating Python’s min()
and max()
. While Python provides you with min()
and max()
to find the smallest and largest values in your data, learning how to do this computation from scratch is a helpful exercise that can help improve your logical thinking and your programming skills.
00:17
In this section of the course, you’ll learn how to find minimum and maximum values in your data. You’ll also learn how to implement your own versions of min()
and max()
.
00:27 To find the minimum value in a small list of numbers as human, you’d normally check the numbers and implicitly compare all of them in your mind. Yes, your brain is pretty amazing, but computers aren’t that smart.
00:39 They need detailed instructions to accomplish any task. You’ll have to tell your computer to iterate over all the values while comparing them in pairs. In the process, the computer has to take note of the current minimum value in each pair until the list of values is exhausted. This explanation may be hard to visualize, so on-screen there’s a Python function that does the work.
01:08
First, you defined find_min()
. For brevity, this function assumes that iterable
isn’t empty and its values are in an arbitrary order. The function treats the first value as a tentative minimum
.
01:21
Then the for
loop iterates over the rest of the elements in the input data. The conditional statement compares the current value
to the tentative minimum
in the first iteration. If the current value
is smaller than minimum
, then the conditional updates minimum
accordingly.
01:39
Each new iteration compares the current value
to the updated minimum
. When the function reaches the end of iterable
, minimum
will hold the smallest value in the input data.
01:51 You’ve coded a function that finds the smallest value in an iterable of numbers.
02:00
Now revisit find_min()
and think how you’d code a function to find the largest value.
02:08
You just have to change the comparison operator from <
to >
, and probably rename the function and some local variables to prevent confusion.
02:17 The new function will look similar to what’s seen on-screen.
02:25
Note that find_max()
shares most of its code with find_min()
. The most important difference, apart from naming, is that find_max()
uses the >
operator instead of the <
operator.
02:44
As an exercise, think of how to avoid repetitive code in find_min()
and find_max()
following the don’t repeat yourself principle.
02:53
This way, you’ll be ready to emulate the complete behavior of min()
and max()
using your Python skills, which which you’ll tackle next.
03:01
To write your custom implementations of min()
and max()
, you’ll start by coding a helper function that’s able to find the smallest or largest value in the input data, depending on the arguments you use in the call.
03:13 Of course, the helper function will especially depend on the operator used for comparing the input values. Your helper function will have the signature seen on-screen.
03:24 Below the signature, you can see what each argument does.
03:30
The body of min_max()
will start by processing *args
to build a list of values. Having a standardized list of values will allow you to write the required algorithm to find the minimum and maximum values in the input data.
03:44
Then the function needs to deal with the key
and default
arguments before computing the minimum and maximum, which is the final step inside min_max()
.
03:54
With min_max()
in place, the final step is to define two independent functions on top of it. These functions will use appropriate comparison operator functions to find the minimum and maximum values, respectively.
04:07 You’ll learn more about operator functions later on, but in the next section of the course, you’ll start creating your new function.
Become a Member to join the conversation.