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"
Please refer to screenshot
Code

Output

Code to copy
def fancy_min(a, b):
if a is None: return b
if b is None: return a
if a < b: return a # if a is small
return b # if b is small
def fancy_max(a, b):
if a is None: return b
if b is None: return a
if a > b: return a # if a is greater
return b # if b is greater
def minimum_of(numbers):
res = None # initializing result
for num in numbers:
res = fancy_min(res,
num) # getting minimum of res and next number
return res
def maximum_of(numbers):
res = None # initializing result
for num in numbers:
res = fancy_max(res,
num) # getting maximum of res and next number
return res
# driver code
def main():
aList = [1, 4, 43, 22, 1223, None, 87, 234,
None, 44]
print("Maximum of aList is:",
maximum_of(aList))
print("Minimum of aList is:",
minimum_of(aList))
main()
Python Lesson Assignment Implement the following 4 functions: fancy_min(a, b) Input: two formal parameters: a and...
Python 3, nbgrader, pandas 0.23.4
Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...
Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. - parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None - leftChild(index) returns the value of the left child of heap[index], returns None if there is no value - rightChild(index) returns...
Use a double for-loop to implement an OR function in python that can combine two Boolean NumPy matrices. (Do not use built-in operators such as | or +). The function should work for one-dimensional and two-dimensional matrices and it should error check to verify input matrix compatibility. Only matrices of the same size can be OR-ed together, if the input to the function is two incompatible matrices, the function should return an error message (using return, not print).
1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...
1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...
Assignment: USING PYTHON Expected submission: TWO (2) Python files, main.py and HelperClasses.py Make a file called HelperClasses and create a class inside it called Helpers Inside Helpers, create 8 static methods as follows: 1.) Max, Min, Standard_Deviation, Mean a.) Each of these should take a list as a parameter and return the calculated value 2.) Bubble a.) This should take a list as a parameter and return the sorted list 3.) Median a.) This should take a list as a...
Need in Python! Write a function definition named power that takes two formal parameters which can be assumed to be any numeric type. The first parameter is called base, the second is called exponent. The function should return the result of the base raised to the exponent power. You may not use any Python math library functions inside your function. You may create your own code to test the function in an IDE if you wish. HINT: The exponent operator in...
Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }
1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...
This is a python work, thank for helping!
7. (6 points) Write a function, countOfAndSmalest Number Between(low, high, ignore, listOfNumbers), that takes as input three numbers and a list of numbers, and returns a list containing two items: 1) how many items in listOfNumbers are greater than low, less than high, and not equal to ignore 2) the smallest number in listOfNumbers that is greater than low, less than high, and not equal to ignore (or None if there is...