Homework 3 (main)
Homework 3 (main)
Homework 3 (main)
Homework 3
(100 points)
Due: Midnight Tuesday Oct 8 on Moodle
For programs, hand-in your Python code AND the tests, with output, you ran to verify that the
program is working correctly. Please write the first three functions by hand, then creating
prompts and using Copilot to create the functions. Be sure to test them in both cases.
Week 5:
1. Write and test a function called convertToCelsius that takes one input (formal parameter) which
will be a temperature in Fahrenheit and returns the temperature in Celsius. The formula to
convert between Fahrenheit and Celsius is as follows (temp_in_F – 32) * 5/9 (10 points)
For example, convertToCelsius (75) should return 23.88…. For this first problem, I will give
you blanks to fill in. The functions in the next problems I will leave for you to do.
def convertToCelsius(__________):
celsius = (______ - 32) * (5/9)
return _______
2. Write and test a function called isNegative. This function should take one number as a formal
parameter. It should return True if the number is odd and False if it is even. (10 points)
For example, isNegative(-5) should return True and isNegative (6) should return False
3. In certain chat and messaging applications there is a limit on the number of characters the
message (a string) can contain. Write a function that takes a String as an input parameter and
returns (not prints) the String unchanged if it is 160 characters or less including all spaces and
punctuation. If it is greater than 160 characters, it should return just the first 160 characters of
the original String. Hints: You can talk a substring of string of any length. You can test with a
string of 16 characters instead of 160. (10 points)
5. Nested Functions. Write prompts for the following two functions (10 points)
a. A function that takes an integer as a formal parameter and returns (not prints) True if the
number is even and returns False otherwise.
b. A function that takes a list of integers as a formal parameter and returns a list containing
only the even numbers from the input list. This function should call the function you
wrote in part a to determine if each number in the list is even. Test the function.
Week 6:
1. (a) For the following, write both an overall purpose of this code and a line-by-
line description. (5 points)
(b) Change the for loop to a while loop so that the same effect is achieved.
(5 points)
2. (a) For the following, write both the overall purpose of this code and a line-by-line
description. (5 points)
total = 0
count = 1
n=5
print(total)
def isOdd(number):
return number % 2 != 0
(b) Change the above program by replacing the while loop with a for loop. The
program should have exactly the same results as before. It should work for ANY
value of n (assuming n is a positive integer).
(5 points)
print(isOdd(-5)) # Expected output: True (since -5 is odd)
print(isOdd(6)) # Expected output: False (since 6 is even)
3. Correct all the errors in the following program, so that it correctly uses a while
loop to add all the integers from 1 to 100 (including 1 and 100). Once the
program is correct, display the final value of total.
(10n points)
total = 0
while (x < 100)
total += x
def limitMessageLength(message):
return message if len(message) <= 160 else message[:160]
(b)Add docstring with doctests to this code and test it. What situation is not being
handled by this code and how would you fix it? (5 points)
5. (a) Write out what each line of the following program does. Then write the
overall purpose of this program.
(5 points)
(b)Add docstring with doctests to this code and test it. (5 points)
def filterEvenNumbers(numbers):
return [num for num in numbers if isEven(num)]
Challenge Problems:
1. The following iterative sequence is defined for the set of positive integers:
n n/2 if n is even
n 3n + 1 if n is odd
Using the rule above, and starting with 13, the following sequence is generated:
13 40 20 10 5 16 8 4 2 1
This sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been
proved, it is thought that all positive integers eventually finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million. (10 points)
count = 1
if count % 2 == 1:
count += 3
def mystery(myList):
left = 0
right = len(myList) -1
while left <= right:
total = 0
n=5