More Function DefinitionsΒΆ
- 
    
Define a function
minmax()that receives a list of numbersnumsas a parameter. The function finds the minimum value in the list and the maximum value in the list, and returns then both in a tuple. If the list is empty, the function returns0for both min and max values.E.g.:
minmax([44, 2, -3, 7]) # returns (-3, 44) minmax([]) # returns (0, 0)
Use
min()andmax()to find the mininum and maximum values in the list.You can return two items by returning them in a tuple: e.g.,
return (1, 2) 
- 
    
Define a function
average()that receives a list of numbersnumsas a parameter. The function returns 0 if the list is empty. Otherwise, it returns the average of the numbers in the list. You may usesum()andlen()to compute the average for the non-empty list.E.g.:
average([3, 4, 5, 6]) # returns 4.5 average([]) # returns 0
First, use an if statement to test if the length is 0. 
- 
    
Define a function
add()that receives two numbersv1andv2as parameters, and also has a third optional parameterv3with a default value of 0. The function returns the sum of the 2 (or 3) values.E.g.:
add(3, 4) # returns 7 add(3, 4, 5) # returns 12
An optional parameter with default value must be at the end of the list of parameters. 
- 
    
Define a function
sqrtsum()that takes 2 required parameters, plus 1 optional parameterdebug. The function returns the sum of the sqrt() of each parameter. Ifdebugis True, it prints the result first before returning it. The default value fordebugisFalse.E.g.:
sqrtsum(4.0, 25) # returns 7.0 sqrtsum(100., 0, True) # prints 10.0 and then returns 10.0
Compute the result into a local variable, say,result. Then write an if statement to check if debug is True. If so, print the result. In either case, return the result. 
- 
    
Define a function
scaleby()that takes 2 parameters: a listinlistand a number,scale. The function returns a new list, which contains each element ininlistmultipled byscale.E.g.:
scaleby([4, -3, 7], 3) # returns [12, -9, 21] scaleby([], 5) # returns [] scaleby([0, 1000, -40], -5) # returns [0, -5000, 200]
Create an empty list calledresult. Then, iterate throughinlist. For each value, multiply it byscaleand append it to the result list. At the end, return that list. 
- 
    
Define a function
constrain()that takes 2 or 3 parameters. The first parameter is a required number. The second parameter,min, is optional, with a default value of -1000000000 (negative 1 billion). The third parameter,max, is optional, with a default value of 1000000000.The function returns the first value such that it has been constrained between
minandmax. Thus, if the value is less thanminit returnsmin. If the value is greater thanmax, it returnsmax. Otherwise, the function returns the value unchanged.Note that if the caller provides values for min and/or max and min > max, the first parameter is returned unchanged.
E.g.:
constrain(3, 0, 10) # returns 3 because 0 < 3 < 10 constrain(10, 50) # returns 50 because 10 < min of 50. constrain(10, 0, 9) # returns 9 because 10 > max of 9. constrain(10, 20, -10) # returns 10 because min > max.
First, test if min is greater than max. If so, return the value.