Question: Python Lesson Assignment Implement the following 4 functions: fancy_min(a, b) Input: two formal parameters: a and b Output: return the minimum of a and b
Python Lesson Assignment
Implement the following 4 functions:
fancy_min(a, b)
Input: two formal parameters: a and b
Output: return the minimum of a and b
Notes:
-
a and b can either be a number or None
-
if a is None, return b
-
if b is None, return a
-
if both are None, return None
-
otherwise return the minimum of a and b
-
do not use the built in function min()
fancy_max(a, b)
Input: two formal parameters: a and b
Output: return the maximum of a and b
Notes:
-
a and b can either be a number or None
-
if a is None, return b
-
if b is None, return a
-
if both are None, return None
-
otherwise return the maximum of a and b
-
do not use the built in function max()
minimum_of(numbers)
Input: numbers is a list of numbers (an item inside numbers can be None as well)
Output: returns the minimum number in numbers.
Notes:
-
minimum_of MUST use the function fancy_min
-
you cannot use the Python min() function
-
if numbers is empty, return None
-
if numbers is None, return None
maximum_of(numbers)
Input: numbers is a list of numbers (an item inside numbers can be None as well)
Output: returns the maximum number in numbers. Notes:
-
maximum_of MUST use the function fancy_max
-
you cannot use the Python max() function
-
if numbers is empty, return None
-
if numbers is None, return None
In all cases, the value None is treated as an absence of a value so the other value always 'wins'.
All your code should go in lesson.py tab/module Use main.py to build your own tests.
Testing
Sometimes it's best to crash when code is not properly working. Python has an assert statement that you can use. The first parameter to assert is a boolean expression. If this boolean expression returns False, a runtime error will be generated. You can also pass a message to print if the assertion is thrown:
# no error will be generated if this passes assert 12 == lesson.minimum_of([12,24,48]), "expected 12"
assert lesson.fancy_min(None, 2) == 2, "should be 2"
assert lesson.fancy_min(2, None) == 2, "should be 2"
assert lesson.fancy_min(2, 3) == 2, "should be 2"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
