1class RunewordsCalculator:
2 def __init__(self, runewords):
3 self.runewords = list(runewords.items())
4 self.runes = []
5 self.used = set()
6
7 def add_runes(self, runes):
8 self.runes.extend(runes)
9
10 def __iter__(self):
11 return self
12
13 def __next__(self):
14 if len(self.used) == len(self.runewords):
15 raise StopIteration
16
17 for name, recipe in self.runewords:
18 if name in self.used:
19 continue
20
21 if self._consume_runes(recipe):
22 self.used.add(name)
23 return name
24
25 return None
26
27 def _consume_runes(self, recipe):
28 indices = []
29 recipe_index = 0
30 for i, rune in enumerate(self.runes):
31 if rune == recipe[recipe_index]:
32 indices.append(i)
33 recipe_index += 1
34
35 if recipe_index == len(recipe):
36 break
37 if recipe_index < len(recipe):
38 return False
39
40 for i in reversed(indices):
41 #not change indexing
42 self.runes.pop(i)
43
44 return True
.....F.
======================================================================
FAIL: test_returns_runewords_in_runeword_order (test.TestRunewordsCalculator.test_returns_runewords_in_runeword_order)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 55, in test_returns_runewords_in_runeword_order
self.assertEqual(next(iterator), "First")
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: None != 'First'
----------------------------------------------------------------------
Ran 7 tests in 0.001s
FAILED (failures=1)