Practice Quiz 4¶
Name: ___________________________________
Question 1 (4 pts)¶
This function returns the lowest \(n\) numbers in a list.
For example, lowest([1, 5, 2, 18, 1, 34], 3) returns [1, 1, 2],
because those are the 3 lowest numbers in the list.
def lowest(data: list[int], n: int) -> list | None:
if len(data) < n:
return None
sorted_list = sorted(data)
result = sorted_list[:n] # the first n numbers
return result
For each test function below, write one assert statement based on the docstring.
def test_3_lowest():
"""Test getting the lowest 3 of 7 numbers in a list."""
Answer: assert lowest([1, 2, 4, 9, 1, 87, 34], 3) == [1, 1, 2]
def test_2_lowest():
"""Test getting the lowest 2 of 4 numbers in a list."""
Answer: assert lowest([1, 2, 9, 3], 2) == [1, 2]
def test_list_too_short():
"""Test getting the lowest 3 of 2 numbers in a list."""
Answer: assert lowest([2, 4], 3) is None
def test_list_empty():
"""Test getting the lowest 1 of 0 numbers in a list."""
Answer: assert lowest([], 1) is None
Question 2 (6 pts)¶
For each code snippet, determine: (1) how many times the loop runs; (2) what is printed on the screen.
Notice that end=" " means each value is printed on the same line, followed by a space.
for n in range(1, 15, 3):
n += 1
if n % 2 == 0:
print(n, end=" ")
How many times the loop runs: 5 What is printed on the screen: 2 8 14
for c in "Go Dukes!":
if c.islower():
print(c, end=" ")
How many times the loop runs: 9 What is printed on the screen: o u k e s
picks = (44, -17, 253)
for i, x in enumerate(picks):
if x > 0:
print(i, x, end=" ")
How many times the loop runs: 3 What is printed on the screen: 0 44 2 253
Question 3 (3 pts)¶
money.py
def get_change(price, paid):
return paid - price
print("Money", end=" ")
if __name__ == "__main__":
print(get_change(3, 4), end=" ")
restaurant.py
import money
BURGER_PRICE = 3
def process_payment(customer, paid):
change = money.get_change(BURGER_PRICE, paid)
print(customer, end=" ")
print(change, end=" ")
process_payment("Ronald", 5)
a. What will be printed when money.py is run?
Money 1
b. What will be printed when restaurant.py is run?
Money Ronald 2
Write all code in a file named prac4.py
Include a module docstring at the top of the file.
Do not type the function docstrings!
Do not use while loops, you must use for loops.
Question 4 (6 pts)¶
def names_cap(names):
"""Create a list of the names with capitalized first letters.
Example:
>>> names_cap(["mayfield", "Sprague", "Shrestha", "wang", "Chao"])
returns ["Sprague", "Shrestha", "Chao"]
Args:
names(list): list of strings.
Returns:
list: Names that begin with a capitalized first letter.
Hint: str.isupper() returns True when called on an entirely uppercase string.
"""
result = []
for name in names:
if name[0].isupper():
result.append(name)
return result
Question 5 (6 pts)¶
def limit_letters(counts, limit):
"""Get letters that don't exceed a count limit.
Ex: limit_letters({"a": 4, "b": 2, "c": 1, "d": 5}, 3) returns
{"b", "c"}, because those letters have a count of 3 or less.
Args:
counts (dict): Maps letters to counts (see count_letters).
limit (int): Maximum value for a letter's count.
Returns:
set: Letters having a count of `limit` or less.
Hint: set() creates an empty set.
"""
result = set()
for letter, count in counts.items():
if count <= limit:
result.add(letter)
return result