Add quiz 3 content, and quiz 3 practice
This commit is contained in:
4
tests/Q3/prefix.py
Normal file
4
tests/Q3/prefix.py
Normal file
@ -0,0 +1,4 @@
|
||||
def with_prefix(prefixes, words):
|
||||
for prefix in prefixes:
|
||||
lst = [word for word in words if word.startswith(prefix)]
|
||||
yield lst
|
41
tests/Q3/test_prefix.py
Normal file
41
tests/Q3/test_prefix.py
Normal file
@ -0,0 +1,41 @@
|
||||
from prefix import with_prefix
|
||||
|
||||
words=["apple","baby","abba"]
|
||||
a_words=["apple", "abba"]
|
||||
def test_simple():
|
||||
assert list(with_prefix(["a"],words)) == [a_words]
|
||||
def test_order():
|
||||
assert list(with_prefix(["b","a"],words)) == [["baby"], a_words]
|
||||
def test_multi():
|
||||
assert list(with_prefix(["bb","ab"],words)) == [[],["abba"]]
|
||||
|
||||
# Commented out because the solution I am submitting is not using regex
|
||||
#def test_regex1():
|
||||
# assert list(with_prefix(["[a-z]b"], words)) == [ ["abba"] ]
|
||||
#def test_regex2():
|
||||
# assert list(with_prefix([".*a$"], words)) == [ ["abba"] ]
|
||||
|
||||
def test_gen():
|
||||
gen = with_prefix(["b"],words)
|
||||
assert next(gen) == ["baby"]
|
||||
|
||||
|
||||
|
||||
|
||||
def test_gen2(): #Testing out of order prefixes, with generator syntax
|
||||
gen = with_prefix(["b", "a"], words)
|
||||
assert next(gen) == ["baby"]
|
||||
assert next(gen) == ["apple", "abba"]
|
||||
|
||||
def test_gen3(): #Testing out returning the same number of elements as words, out of order
|
||||
gen = with_prefix(["bab", "abb", "app"], words)
|
||||
assert next(gen) == ["baby"]
|
||||
assert next(gen) == ["abba"]
|
||||
assert next(gen) == ["apple"]
|
||||
|
||||
|
||||
words2 = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "z"]
|
||||
def test_gen4(): #Testing a long word and one letter word
|
||||
gen = with_prefix(["a", "z"], words2)
|
||||
assert next(gen) == ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
|
||||
assert next(gen) == ["z"]
|
Reference in New Issue
Block a user