Chapter 5b – Flashcards
Unlock all answers in this set
Unlock answersquestion
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.
answer
def square(int):
return int**2
question
Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.
answer
def twice(int):
return 2*int
question
Write the definition of a function add, that receives two int parameters and returns their sum.
answer
def add(int1,int2):
return int1+int2
question
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).
answer
def typing_speed(num_words,time_interval):
num_words>=0
time_interval>0
result=float(60*num_words/time_interval)
return result
question
Write the definition of a function max that has three int parameters and returns the largest.
answer
def max(x,y,z):
if (x>z and x>y):
return (x)
elif (y>x and y>z):
return y
else:
return z
question
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.
answer
def power_to(adouble, aint):
if aint<0:
return 0
else:
return adouble**aint
question
Write the definition of a function add which recieves two parameters containing integer values and returns their sum
answer
def add(int1,int2):
result=int1+int2
return result
question
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.
answer
def oneMore(int):
return int+1
question
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.
answer
def oneLess(int):
return int-1
question
[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.
answer
def powerTo(adouble,aint):
if aint>=0:
return adouble**aint
else:
return 0
question
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.
answer
def hasRealSolution(a,b,c):
if b**2-4*a*c<0:
return False
else:
return True
question
Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.
answer
def absoluteValue(int):
return abs(int)
question
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.
answer
def signOf(int):
if int>0:
return 1
elif int==0:
return 0
else:
return -1
question
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.
answer
def isPositive(int):
if int>0:
return True
else:
return False
question
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.
answer
def isSenior(int):
if int>=65:
return True
else:
return False
question
Define a function called isEven that takes a parameter containing an integer value and returns True if the parameter is even, and False otherwise.
answer
def isEven(int):
if int%2==0:
return True
else:
return False
question
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.
answer
def min(int1,int2):
if int1
question
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.
answer
eurasia_sales =add(euro_sales,asia_sales)
question
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.
answer
cube_volume = to_the_power_of(cube_side,3)
question
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".)
answer
def min(str1,str2,str3):
if str1
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New
Haven't found what you were looking for?