Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
fcef1277f3
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"]
|
32
tests/practice/Q3/practice_questions.py
Normal file
32
tests/practice/Q3/practice_questions.py
Normal file
@ -0,0 +1,32 @@
|
||||
def is_word(word):
|
||||
counter = 0
|
||||
for l in word:
|
||||
if (counter % 2) == 0: #zero is vowel, one is constanant
|
||||
if l == 'a' or l == 'e' or l == 'i' or l == 'o' or l == 'u':
|
||||
counter = counter + 1
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
if l == 'b' or l == 'k' or l == 'p' or l == 't' or l == 'z':
|
||||
counter = counter + 1
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
def cycle(lst):
|
||||
while True:
|
||||
yield lst
|
||||
x = lst[0]
|
||||
lst = lst[1:]
|
||||
lst.append(x)
|
||||
|
||||
class Skippy:
|
||||
def __init__(self, lst, offset)
|
||||
self.lst = lst
|
||||
self.offset = offset
|
||||
self.counter = 0
|
||||
|
||||
def __next__(self)
|
||||
if self.counter > length(self.lst)
|
||||
self.counter = 0
|
||||
|
37
tests/practice/Q3/test_practice_questions.py
Normal file
37
tests/practice/Q3/test_practice_questions.py
Normal file
@ -0,0 +1,37 @@
|
||||
from practice_questions import is_word
|
||||
|
||||
def test_match():
|
||||
assert is_word("akataka") == True
|
||||
assert is_word("ububu") == True
|
||||
assert is_word("ikekezaza") == True
|
||||
|
||||
def test_extra():
|
||||
assert is_word("akatakaa") == False
|
||||
assert is_word("uububu") == False
|
||||
|
||||
def test_bad_letter():
|
||||
assert is_word("yakataka") == False
|
||||
assert is_word("akatakala") == False
|
||||
|
||||
def test_consonant_start():
|
||||
assert is_word("kakataka") == False
|
||||
assert is_word("bububu") == False
|
||||
|
||||
|
||||
from practice_questions import cycle
|
||||
def test_small():
|
||||
lst = [1,2,3]
|
||||
g = cycle(lst)
|
||||
assert next(g) == lst
|
||||
assert next(g) == [2,3,1]
|
||||
assert next(g) == [3,1,2]
|
||||
|
||||
def test_big():
|
||||
n = 5000
|
||||
lst = list(range(n))
|
||||
g = cycle(lst)
|
||||
for j in range(n):
|
||||
lst2 = next(g)
|
||||
assert lst2[0] == n-1
|
||||
lst3 = next(g)
|
||||
assert lst3==lst
|
Loading…
Reference in New Issue
Block a user