Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Borrowed Solutions Annotated solution in Clear category for Min and Max by BootzenKatzen
# I got this straight from the hints because my brain was not cooperating with me today.
# But now that I see it, it makes total sense.
def get_first_from_sorted(args, key, reverse): # This is a function to return the first item from a sorted list
# having the args, key, reverse allows us to chose the direction
# of the sorting when we call the function
if len(args) == 1: # if the length of the arguments is 1, it means that we're
# looking at a list object ([1, 2, 3]) instead of a series of
# numbers (1, 2, 3), so the length is 1 - one list
args = iter(args[0]) # so if we have a list, iter() will iterate through the list
# and return each value, so our args are now all the numbers
# inside the list
return sorted(args, key=key, reverse=reverse)[0] # returns the [0] position item from the sorted list
# where reverse is = to the reverse argument entered
def min(*args, key=None):
return get_first_from_sorted(args, key, False) # runs get_from_sorted with reverse = False
# so it will sort smallest to largest then return the [0]
# position number which is the smallest - the minimum
def max(*args, key=None):
return get_first_from_sorted(args, key, True) # runs get_from_sorted with reverse = True
# so it will sort from largest to smallest then return
# the [0] position item, which in this case is the larges
# so we get the maximum
# This is veky's "Best Creative Solution"
# I was intregued so I had to try and work through it
min2, max2 = (lambda *args, key=None, r=r: sorted(args[0] if len(args) == 1
else args, key=key, reverse=r)[0] for r in range(2))
"""
I thought this was really clever overall. I'm breaking it down as a mental exercise to understand.
We're able to assign functions to min and max at the same time because of the "for r in range(2)" at the end
min comes up first, so the first number that comes up is 0 so r = 0 for min
we see 'r' referensed as "reverse" and since 0 is the same as "False" to python
for min reverse will be false, and it will sort smallest to largest then the [0] (near the range bit)
will take the first item - the smallest- to get the minimum
max comes up second, so the next r value is 1 which python will read as True
so in this case reverse will be = True, so it will sort largest to smallest
and the [0] will pull the first item, which is the largest in this case to get the max
To go back toward the beginning to tackle the rest of this
lambda is what establishes it as a function and *args, key=None, r=r: are the syntax of the function
*args can accept a variable number of arguments
if we just use args in this place, then if we have min(1, 2, 3, 4) we get an error
that int does not have a length. *args can pull them all in under args, then we can
check the length of the list of arguments
we need the key for some of the asserts - they're adding conditions so we could find
the max and min in different ways like sorting them as integers, or finding
the list pair with the smallest [1] value.
r as we've already discussed is for reverse, so we can get min or max
then we have sorted() which will sort our arguments based on the syntax
inside sorted we have args[0] if len(args) == 1
which will catch cases like min([1, 2, 3]) since [1, 2, 3] is technically a list object
the length of the list of arguments is one - one list
so sorted(args[0]) would sort what's in that list
then we have else args, which would be for normal usage without list objects
so if the length of the list of arguments is greater than 1 it will sort all the arguments presented
then key and reverse just call up the syntax used.
If you don't call an additional key when using min or max, the key is None
and it will sort normally. R we've already discussed. It's being decided by
the r in range based on the position of the function names.
"""
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert max(3, 2) == 3, "Simple case max"
assert min(3, 2) == 2, "Simple case min"
assert max([1, 2, 0, 3, 4]) == 4, "From a list"
assert min("hello") == "e", "From string"
assert max(2.2, 5.6, 5.9, key=int) == 5.6, "Two maximal items"
assert min([[1, 2], [3, 4], [9, 0]], key=lambda x: x[1]) == [9, 0], "lambda key"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
June 9, 2023
Comments: