Programming Homework Ch. 5

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

def add(int1,int2): return int1+int2

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

def add(int1,int2): sum=int1+int2 return sum

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(wang_help_me): return int(wang_help_me/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 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

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

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 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(x): if x < 2: return False else: for n in range(2,x): if x % n == 0: return False return True

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

def max(int1,int2,int3): if int1>int2 and int1>int3: return int1 elif int2>int1 and int2>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(int1,int2): if int1<int2: return int1 elif int2<int1: return int2 else: return int1 or int2

Write a function mymin 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(str1,str2): if str1>str2: return str2 else: return str1

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(double,int): if int>=0: return double**int else: return 0

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 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,int): if int<0: return 0 else: return double**int

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(".....")

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(grade): print("Grade:",grade)

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 if int==0: return 0 if int<0: return -1

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

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 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 n==20: k=3 elif (is_prime(i)): sum+=1 k+=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)

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 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_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)


Ensembles d'études connexes

Producers,Consumers and Decomposers

View Set

Pharmacology General Principles: Chapter 4 & 5

View Set

AP Gov Unit 3 : Civil Liberties and Civil Rights

View Set

English midterm review guide (Duran)

View Set