Quiz Review Questions
Conceptual Questions
Dictionaries
- Dictionaries in Python can have duplicate keys. (T/F)
- Dictionaries in Python can be nested, meaning a dictionary can contain another dictionary as a value. (T/F)
Unit Tests
- All pytest test function names must begin with
test_
. (T/F) - If a unit test does NOT pass, this means that one of the assertions in the test is False. (T/F)
- Test functions should be written in a file with a name followed by
_test.py
. (T/F) - Pytest is the only unit testing framework available for Python. (T/F)
- Unit tests in Pytest are written in separate files from the code they are testing. (T/F)
Runtime Analysis
- A function with a big-O notation of O(n) will always run faster than a function with a big-O notation of O(n^2) for all input sizes. (T/F)
- Big-O notation considers the constants and lower-order terms when analyzing the runtime of an algorithm. (T/F)
- Big-O notation can be used to analyze both time complexity and space complexity of algorithms. (T/F)
- Big-O notation provides a precise measurement of the actual runtime of an algorithm on a specific machine. (T/F)
- Big-O notation accounts for variations in performance due to factors like hardware and compiler optimizations. (T/F)
- If an algorithm has a time complexity of O(1), it means its runtime is constant and independent of the input size. (T/F)
- What does it mean for an input to be a “worst case” input?
- What is a worst-case input for a
search
function? - What is a worst-case input for a
sort
function?
Unit Tests Short Response
- This function finds the first word in a
list
ofstr
with an evenlength.
def find_even(words: list[str]) -> str:
idx: int = 0
while idx < len(words):
if len(words[idx]) % 2 == 0:
return words[idx]
idx += 1
return ""
Fill in this unit test with a use case.
def test_find_even_use_case() -> None:
""" Put code here. """
- This function removes the first word in a
list
ofstr
with an even length.
def remove_first_even(words: list[str]) -> None:
idx: int = 0
condition: bool = True
while (idx < len(words)) and condition:
if len(words[idx]) % 2 == 0:
words.pop(idx)
condition = False
idx += 1
Fill in this unit test with a use case.
def test_remove_first_even_use_case() -> None:
""" Put code here. """
Function Writing
value_exists
:
- The function name is value_exists and is called with a
dict[str,int]
and anint
as an argument. - The function should return a
bool
. - The function should return
True
if theint
exists as a value in the dictionary, andFalse
otherwise. - The function should not mutate (modify) the input dict.
- Explicitly type variables, parameters, and return types.
- The following REPL examples demonstrate expected functionality of your
value\_exists
function:
>>> test_dict: dict[str,int] = {"a": 2, "b": 4, "c": 7, "d": 1} >>> test_val: int = 4 >>> value_exists(test_dict, test_val) True >>> value_exists(test_dict, 5) False
plus_or_minus_n
:
- The function name is
plus_or_minus_n
and is called withinp: dict[str,int]
andn: int
as an argument. - The function should return
None
. It instead mutates the input dictionaryinp
. - The function should check if each value in
inp
is even or odd. If it is even, addn
to that value. If it is odd, subtractn
. - Explicitly type variables, parameters, and return types.
- The following REPL examples demonstrate expected functionality of your function:
>>> test_dict: dict[str,int] = {"a": 2, "b": 4, "c": 7, "d": 1} >>> test_val: int = 4 >>> plus_or_minus_n(test_dict, test_val) >>> test_dict {"a": 6, "b": 8, "c": 3, "d": -3}
Memory Diagrams
Focus on the Dictionary memory diagrams on the practice memory diagram page.
Quiz Review Solutions
Conceptual Questions Solutions
Dictionaries
- False. It can have duplicate values, but the keys must be unique.
- True. E.g.
example: dict[str, dict[str, str]] = {"a": {"Hello": "World"}}
Unit Tests
- True
- True
- True
- False. It is the one we use for this class, but there are other frameworks!
- True
Runtime Analysis
False. n IS faster that n2, but given that this is Big-O notation, it’s only considering the worst case input. In other words, it’ll run faster for a worst-case input, but not necessarily for every possible input.
False. For example if a runtime is computed to be O(n2 + 2n + 3), that’ll simply be represented as O(n2).
True
False. It’s a general performance approximation in terms of input size.
False.
True.
This is an input that will result in the longest possible runtime of the algorithm. In other words, this is the input that will reflect the Big-O runtime.
Searching for an element that is not in the list. This means you’ll have to look at the entire list before realizing that it’s not there.
A descending list. This means every element is in the wrong location and will need to be moved. (This is what you do in the runtime exercise.)
Unit Tests Short Response Solutions
These can have many possible answers! Here is just one example.
def test_find_even_use_case() -> None:
test_input: list[str] = ["Hello", "Hi", "Howdy", "Hiya"]
assert find_even(test_input) == "Hi"
def test_remove_first_even_use_case() -> None:
test_input: list[str] = ["Hello", "Hi", "Howdy", "Hiya"]
remove_first_even(test_input)
assert test_input == ["Hello", "Howdy", "Hiya"]
Function Writing Solutions
value_exists
:
def value_exists(d: dict[str, int], num: int) -> bool:
for key in d:
if d[key] == num:
return True
return False
def value_exists(d: dict[str, int], num: int) -> bool:
exists: bool = False
for key in d:
if d[key] == num:
exists = True
return exists
plus_or_minus_n
:
def plus_or_minus_n(inp: dict[str, int], n: int) -> None:
for key in inp:
if inp[key] % 2 == 0:
inp[key] = inp[key] + n
else: # element is odd
inp[key] = inp[key] - n
def plus_or_minus_n(inp: dict[str, int], n: int) -> None:
for key in inp:
if inp[key] % 2 == 0:
inp[key] += n
else: # element is odd
inp[key] -= n