Hi everyone,
Hope you are doin good. I have a few questions regarding an exercise I’ve found in my python course.
The topic is “Conditionals” and this is the exercise: # Write a program so that it finds the largest and smallest values in the list no matter what the range of values originally is
values = [...some test data...]
smallest, largest = None, None
for v in values:
if ____:
smallest, largest = v, v
____:
smallest = min(____, v)
largest = max(____, v)
print(smallest, largest)
And here is the answer
values = [2, -2.8, -88, 55, 46, 0,4]
smallest, largest = None, None
for v in values:
if v is None or largest is None:
smallest, largest = v, v
else:
smallest = min(smallest, v)
largest = max(largest, v)
print(smallest, largest)
However, there are a few things not clear to me such as the use of the value None and the idea behind its use.
what type of object is smallest, largest = None, None I thought you are not allowed to create names using spaces and here I see a space and a comma.
what am I actually claming with if v is None or largest is None:
why the min and max values in parenthesis smallest = min(smallest, v) largest = max(largest, v)
report two objects?
smallest, largest = None, None can be thought of as a tuple unpacking into another tuple of named variables: (smallest, largest) = (None, None). A tuple in Python does not actually require parenthesis in many situations, which can be a little confusing to new users. So essentially, you are initializing both variables to be None.
v is None is a little odd because your array (values) contains no None and I’m not sure what the purpose is for allowing such. If a None is in values then when you check v, you will lose your current smallest or largest value. Personally, I’d probably throw an error if v is None or skip the value, but I don’t know the intent of the code.
The check for largest is None essentially makes sure you store an initial largest and smallest value if you haven’t already. You can’t compare a number with None either, so you need to avoid that check if you don’t have real values in those variables. The first value you check is both your largest and smallest as it the first and only value you’ve checked. Also, values is empty, then smallest and largest will still be None meaning you found no numbers that are biggest or smallest.
Lastly, I’m not sure about your last question, min and max are functions and you are passing in two values to see which of the two is the minimum or maximum respectively to update your variables with said minimum and maximum.
Here is (untested) better code, which works with values being any iterable of numbers. It does not catch non-number values. (I realize that you might be constrained to the template provided.)
def minmax(values):
values = iter(values) # Trick of using iter to special case first item is very handy sometimes.
try:
small = large = next(values) # Invariant: small <= large
except StopIteration: # No values.
return None, None
for v in values: # Starts with 2nd value, if any.
if v < small:
small = v
elif v > large: # v cannot be both smallest and largest.
large = v
return small, large
Putting code in a function enables automated testing.