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 integer greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an integer greater than zero). The function returns the typing speed of that person in words per minute (a float).

2. Write the definition of a function twice, that receives an integer parameter and returns an integer that is twice the value of the parameter.

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

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

5. Write the definition of a function power_to, which receives two parameters. The first is a float and the second is an integer. The function returns a float.

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.

6. 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 the total exceeds n. Associate this number with the variable k.

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

8. 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.

9. Given that add, a function that expects two integer 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.

10. 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.

11. Assume that to_the_power_of is a function that expects two integer 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.

12. max is a function that expects two integer parameters and returns the value of the larger one.

Two variables, population1 and population2, have already been defined and associated with integer values.

Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.

13. Assume that max2 is a function that expects two integer parameters and returns the value of the larger one.

Also assume that four variables, population1, population2, population3, and population4 have already been defined and associated with integer values.

Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max2.

14. Assume that print_error_description is a function that expects one integer parameter and returns no value.

Write a statement that invokes the function print_error_description, passing it the value 14.

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

16. Write the code to call a function whose name is send_number. There is one argument for this function, which is an integer. Send 5 as an argument to the function.

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

18. Write the code to call a function named send_variable and that expects a single integer parameter.

Suppose a variable called x refers to an integer. Pass this variable as an argument to send_variable.

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

20. Write a function to compute the following series:

m(i) = 1/2 + 2/3 + ... + i/(i+1)

Write a test program that displays the following table:

i m(i)
1 0.5000
2 1.1667
...
19 16.4023
20 17.3546

21. (Financial application: compute the future investment value)

Write a function that computes a future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula in Programming Exercise 2.19 in Chapter 2 Programming Exercise from the Book.

Use the following function header:

def futureInvestmentValue(investmentAmount, monthlyInterestRate, years):

For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59.

Write a test program that prompts the user to enter the investment amount and the annual interest rate in percent and prints a table that displays the future value for the years from 1 to 30.

Sample Run

The amount invested: 1000
Annual interest rate: 9
Years Future Value
1 1093.80
2 1196.41
...
29 13467.25
30 14730.58

22. (Occurrences of a specified character)

Write a function that finds the number of occurrences of a specified character in the string using the following header:

def count(s, ch):

For example, count("Welcome", 'e') returns 2. The str class has the count method. Implement your function without using the count method. Write a test program that reads a string and a character and displays the number of occurrences of the character in the string.

Sample Run

Enter a string: Welcome to Python
Enter a character: o
o appears in Welcome to Python 3 times

23. (Convert milliseconds to hours, minutes, and seconds)

Write a function that converts milliseconds to hours, minutes, and seconds using the following header:

def convertMillis(millis):

The function returns a string as hours:minutes:seconds.

For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10.

Write a test program that prompts the user to enter a value for milliseconds and displays a string in the format of hours:minutes:seconds.

Sample Run

Enter time in milliseconds: 555550000
154:19:10

24. (Binary to hex)

Write a function that parses a binary number into a hex number. The function header is:

def binaryToHex(binaryValue)

Write a test program that prompts the user to enter a binary number and displays the corresponding hexadecimal value. Use uppercase letters A, B, C, D, E, F for hex digits.

Sample Run

Enter a binary number: 10001111
The hex value is 8F
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.