UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ sample3

Akataka (regular expressions)

In the constructed language =Akataka=, every word starts with a vowel (a, e, i, o, or u) and is followed by a non-empty sequence of alternating consonant, vowel pairs. An example of a valid word in Akataka is akataka. In Akataka there are only 5 consonants: b, k, p, t, and z.

Use one or more regular expressions to write a function is_word that recognizes words in Akataka. You may assume all input is lower-case.

Your code should pass the following tests. Do not hardcode the strings from the tests in your function.

  from akataka 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

Cyclic permutations (generators)

Write a python generator called cycle that rotates the current list one position to the left each time next is called. I recommend using list slicing to do the actual rotation. Your generator should pass the following tests.

from cycle 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

Multiples (list comprehensions)

Use list comprehensions (and if possible only list comprehensions) to write a function multiples, that given a parameter n, for each j from 1 to n returns a list of all multiples of j that are less than or equal to n. In particular your function should pass the following tests.

  from multiples import multiples

  def test_1():
      assert multiples(1)== [ [1] ]

  def test_4():
      assert multiples(4)==[[1, 2, 3, 4], [2, 4], [3], [4]]

  def test_10():
      assert multiples(10) == [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                               [2, 4, 6, 8, 10], [3, 6, 9],
                               [4, 8], [5, 10],
                               [6], [7], [8], [9], [10]]

Skippy (iterator classes)

Iterators are covered in L18

Write a Python iterator class Skippy whose constructor takes a list lst and an offset k, and returns every kth element in lst. Your iterator should treat lst as circular, wrapping around when it reaches the end. Your iterator class should

from skippy import Skippy

def test_wrap():
    skipper = Skippy(["Alice","Bob","Mallory","Sam","Tom"],2)
    assert [ next(skipper) for _ in range (7) ] \
    == ["Alice", "Mallory", "Tom", "Bob","Sam", "Alice", "Mallory"]

def test_reset():
    skipper = Skippy(list(range(10)),3)
    (res1,res2) = [],[]
    for j in skipper:
    res1.append(j)
    if len(res1) > 7:
       break
    for j in skipper:
    res2.append(j)
    if len(res2) > 7:
       break

    assert res1==res2