Chapter 5b
Write the definition of a function max that has three int parameters and returns the largest.
def max(x,y,z): if (x>z and x>y): return (x) elif (y>x and y>z): return y else: return z
Define a function called signOf that takes a parameter containing an integer value and returns a 1 if the parameter is positive, 0 if the parameter is 0, and -1 if the parameter is negative.
def signOf(int): if int>0: return 1 elif int==0: return 0 else: return -1
Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float).
def typing_speed(num_words,time_interval): num_words>=0 time_interval>0 result=float(60*num_words/time_interval) return result
Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.
def absoluteValue(int): return abs(int)
Write the definition of a function add which recieves two parameters containing integer values and returns their sum
def add(int1,int2): result=int1+int2 return result
Write the definition of a function add, that receives two int parameters and returns their sum.
def add(int1,int2): return int1+int2
Define a function called hasRealSolution that takes three parameters containing integer values: a, b, and c. If "b squared" minus 4ac is negative, the function returns False otherwise, it returns True.
def hasRealSolution(a,b,c): if b**2-4*a*c<0: return False else: return True
Define a function called isEven that takes a parameter containing an integer value and returns True if the parameter is even, and False otherwise.
def isEven(int): if int%2==0: return True else: return False
Define a function called isPositive that takes a parameter containing an integer value and returns True if the paramter is positive or False if the parameter is negative or 0.
def isPositive(int): if int>0: return True else: return False
Define a function called isSenior that takes a parameter containing an integer value and returns True if the parameter is greater than or equal to 65, and False otherwise.
def isSenior(int): if int>=65: return True else: return False
Define a function called min that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.
def min(int1,int2): if int1<int2: return int1 elif int2<int1: return int2 else: return int1 or int2
Write a function min that has three str parameters and returns the smallest. (Smallest in the sense of coming first alphabetically, not in the sense of "shortest".)
def min(str1,str2,str3): if str1<str2 and str2<str3: return str1 elif str2<str1 and str1<str3: return str2 else: return str3
Write the definition of a function oneLess which recieves a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.
def oneLess(int): return int-1
Write the definition of a function oneMore which recieves a parameter containing an integer value and returns an integer that is one more than the value of the parameter.
def oneMore(int): return int+1
[Functions >> functions and if statements] Write the definition of a function powerTo which recieves two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0.
def powerTo(adouble,aint): if aint>=0: return adouble**aint else: return 0
Write the definition of a function power_to, which receives two parameters. The first is a double and the second is an int. The function returns a double. If the second parameter is negative, the function returns zero. Otherwise it returns the value of the first parameter raised to the power of the second.
def power_to(adouble, aint): if aint<0: return 0 else: return adouble**aint
Write the definition of a function square which recieves a parameter containing an integer value and returns the square of the value of the parameter.
def square(int): return int**2
Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.
def twice(int): return 2*int
Assume that to_the_power_of is a function that expects two int parameters and returns the value of the first parameter raised to the power of the second parameter. Write a statement that calls to_the_power_of to compute the value of cube_side raised to the power of 3 and that associates this value with cube_volume.
cube_volume = to_the_power_of(cube_side,3)
Given that add, a function that expects two int parameters and returns their sum, and given that two variables , euro_sales and asia_sales, have already been defined: Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.
eurasia_sales =add(euro_sales,asia_sales)