MyProgrammingLab Starting out with Python Ch.5

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write a function min that has two str parameters and returns the smallest. (Smallest in the sense of coming first alphabetically, not in the sense of "shortest".)

def mymin(s1, s2): if s1 < s2: return s1 else: return s2

Write the definition of a function printGrade, which takes one parameter containing a string value and returns nothing. The function prints "Grade: " followed by the string parameter.

def printGrade(x): print("Grade:",(x))

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 code to call a function whose name is send_number. There is one argument for this function, which is an int. Send 5 as an argument to the function.

send_number(5)

Write the code to call a function named send_object and that expects one parameter, of type Customer. Suppose there is an object of type Customer, referred to by John_Doe. Use this object as an argument to the function.

send_object(John_Doe)

Write the code to call a function named send_two and that expects two parameters: a float and an int. Invoke this function with 15.955 and 133 as arguments.

send_two(15.955,133)

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)

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(x): return abs(x)

Write the definition of a function add, that receives two int parameters and returns their sum.

def add(x, y): sum = x+y return sum

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(x): if x%2==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(x): if x>=65: return True else: return False

Write the definition of a function max that has three int parameters and returns the largest.

def mymax(int1, int2, int3): if int1 > int2 > int3: return int1 elif int2 > int1 > int3: return int2 else: return int3

Define a function called mymin that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.

def mymin(x,y): if x <y: return x else: return y

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 int*2

Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n. Associate this number with the variable k.

i = 2 k = 0 sum = 0 while (sum+i <=n): if(is_prime(i)): sum+=i k+=1 i+=1

Given print_larger, a function that expects two parameters and returns no value and given two variables, sales1 and sales2, that have already been defined, write a statement that calls print_larger, passing it sales1 and sales2.

print_larger(sales1, sales2)

Assume that print_todays_date is a function that uses no parameters and returns no value. Write a statement that calls (invokes) this function.

print_todays_date()

Write the definition of a function add which recieves two parameters containing integer values and returns their sum.

def add (x, y): return x+y

Write the definition of a function half which recieves a variable containing an integer as a parameter, and returns another variable containing an integer, whose value is closest to half that of the parameter. (Use integer division!)

def half(x): return int(x/2)

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 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(x): if x > 0: return True else: return False

Define a function is_prime that receives an integer argument and returns true if the argument is a prime number and otherwise returns false. (An integer is prime if it is greater than 1 and cannot be divided evenly [with no remainder] other than by itself and one. For example, 15 is not prime because it can be divided by 3 or 5 with no remainder. 13 is prime because only 1 and 13 divide it with no remainder.) This function may be written with a for loop, a while loop or using recursion.

def is_prime(n): if n < 2:return False i = 2 while i < n: if n % i == 0:return False i+=1 return True

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(x): return x-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(i): return i+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(x,y): if y >= 0: return x**y 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(double, int1): if int1 < 0: return 0 else: return double**int1

Write a definition of the function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line consisting of 5 periods (".").

def printDottedLine(): print(".....")

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(x): if x>0: return 1 elif x ==0: return 0 else: return 0-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(words_typed,time_spent_typing): words_typed>=0 time_spent_typing>0 return((words_typed/time_spent_typing)*60)

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)

Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total. Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.

i = 2 count = 0 total = 0 while (count < n): if (is_prime(i)): total+=i count = count +1 i+=1

Assume that print_error_description is a function that expects one int parameter and returns no value. Write a statement that invokes the function print_error_description, passing it the value 14.

print_error_description(14)

Write the code to call the function named send_signal. There are no parameters for this function.

send_signal()

Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable.

send_variable(x)


Conjuntos de estudio relacionados

Unit 2 SOCIO: Race and Ethnicity

View Set

Com Arts 100: Chapter 7 (Gathering Materials)

View Set

Chapter 5 Cost Approach - Cost Estimating

View Set

All Nutrition Quizzes for Test #1

View Set

Wiley Plus Chapter 25 Animations & Test Bank

View Set