Quiz 4
Name: ___________________________________
Question 1 (4 pts)¶
This function creates a dictionary of the occurrence of the letter q, v, z in a string.
For example, scrabble_words("squeeze quiz violin") returns {'q':2, 'v':1, 'z':2},
because there are 2 'q's, one ''v', and 2 'z''s in 'squeeze quiz violin'.
def scrabble_words(phrase):
if not phrase:
return None
res = {}
for letter in "qvz":
count = phrase.count(letter)
if count > 0:
res[letter] = count
return res
For each test function below, write one assert statement based on the docstring.
def test_no_qvz():
"""Test checking a string with no words containing q, v, or z."""
Answer: assert scrabble_words("apple orange") == {}
def test_empty_string():
"""Test checking an empty string."""
Answer: assert scrabble_words("") is None
def test_all_qvz():
"""Test a string with at least one occurrence of q, v, and z."""
Answer: assert scrabble_words("squeeze quiz violin") == {'q':2, 'v':1, 'z':2}
def test_3_q():
"""Test a string with 3 q's"""
Answer: assert scrabble_words("queen squeeze quiet") == {'q':3, 'z':2}
Question 2 (4 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 val in range(3, 18, 3):
if val % 6 == 0:
print(n+1, end=" ")
How many times the loop runs: 5 What is printed on the screen: 7 13
for c in "vat is zero":
if c in ['q', 'v', 'z']:
print(c, end="-")
How many times the loop runs: 11 What is printed on the screen: v-z-
Question 3 (5 pts)¶
quotas.py
def remain_quota(tup):
# Get the duration between the
# tuple's start and end time.
return tup[0] - tup[1]
if __name__ == "__main__":
print("Remaining", end=" ")
print(remain_quota((8000, 2000)), end=" ")
compare.py
import quotas
def compare(accounts):
largest = 0
final = ""
for acct in accounts:
acct_remain = quotas.remain_quota(accounts[acct])
if acct_remain > largest:
largest = acct_remain
final = acct
return final, largest
if __name__ == "__main__":
data = {"Jim": (1000, 500), "Bill": (4000, 1500)}
result = compare(data)
print(result[0], result[1])
a. What will be printed when quotas.py is run?
Remaining 6000
b. What will be printed when compare.py is run?
Bill 2500
Name: ___________________________________
| DO | DO NOT |
|---|---|
|
|
Question 4 (5 pts)¶
def times_table(base, limit):
"""Return a list for the times table for base from 1 X base up to and including limit X base.
The base and limit valid values are from 1 to 12. If the base and limit are not in this range, return None.
Example:
>>> times_table(4, 12)
returns [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48]
Args:
base (int): times table base
limit (int): the range of the table
Returns:
list: the times table for base from 1 to limit
or None if there is an error.
"""
def times_table(base, limit):
if base not in range (1,13) or limit not in range(1,13):
return None
res =[]
for x in range(1,limit+1, 1) :
res.append(base * x)
return res
def times_table(base, limit):
if base <1 or base >12 or limit <1 or limit > 12:
return None
res =[]
for x in range(base*1,base * (limit+1), base) :
res.append(x)
return res
Question 5 (7 pts)¶
def jumping_pups(data, height):
"""Create a list of dogs who can jump at least height inches.
Ex: jumping_pups({"cassie": 61, "emma": 50, "cleo": 10, "rusty": 14}, 50)
returns , ['cassie', 'emma'] because those dogs can jump 50 inches or higher.
Args:
data (dict): Maps dog names to their jumping ability in inches.
height (int): Minimum inches for height of jump
Returns:
list: Names of the dogs who can jump height or higher.
"""
def jumping_pups(data, height):
res = []
for dog in data:
if data[dog] >= height:
res.append(dog)
return res