Homework 3 (main)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

CMSC 150 Fall 2024

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)

Start using ChatGPT


4. Write a prompt to create a function that takes two integer parameters and returns how many
three-digit numbers (100 – 999) are divisible by both of them.
(Evenly divisible means that there is no remainder when dividing by either number ). Turn in
the prompt, and the program. For example, if 7 and 3 are passed in as parameters, then 43
should be returned . (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.

Be sure to to hand in the prompts, the program, and your tests.

Week 6:

1. (a) For the following, write both an overall purpose of this code and a line-by-
line description. (5 points)

for count in range(1, 20, 3):


if (count % 2 == 1):
print(count, "is odd")
def convertToCelsius(temp_in_F):
celsius = (temp_in_F - 32) * (5 / 9)
return celsius

(b) Change the for loop to a while loop so that the same effect is achieved.
(5 points)

print(convertToCelsius(75)) # Expected output: 23.888...

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

while count <= n:


total += count
count += 2

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]

print(limitMessageLength("This is a test message.")) # Should return the full string since


it's under 160 chars
print(limitMessageLength("A" * 200)) # Should return first 160 "A"s only
4. (a) Write out what each line of the following program does. Then write the
overall purpose of this program.
(5 points)
def mystery(n):
total = 1
for number in range(1, n):
total *= number
return total

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

def countDivisibleByBoth(a, b):


count = 0
for num in range(100, 1000):
if num % a == 0 and num % b == 0:
count += 1
return count

print(countDivisibleByBoth(7, 3)) # Expected output: 43

5. (a) Write out what each line of the following program does. Then write the
overall purpose of this program.
(5 points)

def mystery2(list1, list2):


out = []
if len(list2) > len(list1):
temp = list2[:]
list2 = list1[:]
list1 = temp[:]
for index in range(len(list1)):
if index < len(list2):
out.append(list1[index] + list2[index])
else:
out.append(list1[index])
return out
def isEven(number):
return number % 2 == 0

(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

while count < 20:

if count % 2 == 1:

print(count, "is odd")

count += 3

2. Read the following function and explain what it is doing

def mystery(myList):
left = 0
right = len(myList) -1
while left <= right:
total = 0
n=5

for count in range(1, n + 1, 2):


total += count
print(total)
midpoint = (left + right) // 2
if midpoint == myList[midpoint]:
return midpoint
elif midpoint < myList[midpoint]:
right = midpoint - 1
else: # myList[midpoint] < midpoint
left = midpoint + 1
return -1

You might also like