From e091a825ce483fba4928515d7aeb59c5ce8758f0 Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 23 Nov 2022 10:09:24 -0400 Subject: [PATCH] Add quiz 3 content, and quiz 3 practice --- tests/Q3/prefix.py | 4 ++ tests/Q3/test_prefix.py | 41 ++++++++++++++++++++ tests/practice/Q3/practice_questions.py | 32 +++++++++++++++ tests/practice/Q3/test_practice_questions.py | 37 ++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 tests/Q3/prefix.py create mode 100644 tests/Q3/test_prefix.py create mode 100644 tests/practice/Q3/practice_questions.py create mode 100644 tests/practice/Q3/test_practice_questions.py diff --git a/tests/Q3/prefix.py b/tests/Q3/prefix.py new file mode 100644 index 0000000..e067c1e --- /dev/null +++ b/tests/Q3/prefix.py @@ -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 diff --git a/tests/Q3/test_prefix.py b/tests/Q3/test_prefix.py new file mode 100644 index 0000000..7f9edf8 --- /dev/null +++ b/tests/Q3/test_prefix.py @@ -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"] diff --git a/tests/practice/Q3/practice_questions.py b/tests/practice/Q3/practice_questions.py new file mode 100644 index 0000000..5de550e --- /dev/null +++ b/tests/practice/Q3/practice_questions.py @@ -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 + diff --git a/tests/practice/Q3/test_practice_questions.py b/tests/practice/Q3/test_practice_questions.py new file mode 100644 index 0000000..d94bf65 --- /dev/null +++ b/tests/practice/Q3/test_practice_questions.py @@ -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