1class RunewordsCalculator:
2 def __init__(self, runewords):
3 self.runewords = runewords
4 self.runes = []
5 self.returned = set()
6
7 def add_runes(self, runes):
8 self.runes.extend(runes)
9
10 def __iter__(self):
11 return self
12
13 def _make_recipe(self, recipe):
14 used_index = []
15 recipe_index = 0
16
17 for rune_index, rune in enumerate(self.runes):
18 if recipe_index < len(recipe) and rune == recipe[recipe_index]:
19 used_index.append(rune_index)
20 recipe_index += 1
21
22 if recipe_index != len(recipe):
23 return False
24
25 for index in reversed(used_index):
26 self.runes.pop(index)
27
28 return True
29
30 def __next__(self):
31 for name, recipe in self.runewords.items():
32 if name not in self.returned and self._make_recipe(recipe):
33 self.returned.add(name)
34 return name
35
36 if len(self.returned) == len(self.runewords):
37 raise StopIteration
38
39 return None
.....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)
| f | 1 | class RunewordsCalculator: | f | 1 | class RunewordsCalculator: |
| 2 | def __init__(self, runewords): | 2 | def __init__(self, runewords): | ||
| 3 | self.runewords = runewords | 3 | self.runewords = runewords | ||
| 4 | self.runes = [] | 4 | self.runes = [] | ||
| 5 | self.returned = set() | 5 | self.returned = set() | ||
| 6 | 6 | ||||
| 7 | def add_runes(self, runes): | 7 | def add_runes(self, runes): | ||
| 8 | self.runes.extend(runes) | 8 | self.runes.extend(runes) | ||
| 9 | 9 | ||||
| 10 | def __iter__(self): | 10 | def __iter__(self): | ||
| 11 | return self | 11 | return self | ||
| 12 | 12 | ||||
| 13 | def _make_recipe(self, recipe): | 13 | def _make_recipe(self, recipe): | ||
| 14 | used_index = [] | 14 | used_index = [] | ||
| 15 | recipe_index = 0 | 15 | recipe_index = 0 | ||
| 16 | 16 | ||||
| 17 | for rune_index, rune in enumerate(self.runes): | 17 | for rune_index, rune in enumerate(self.runes): | ||
| 18 | if recipe_index < len(recipe) and rune == recipe[recipe_index]: | 18 | if recipe_index < len(recipe) and rune == recipe[recipe_index]: | ||
| 19 | used_index.append(rune_index) | 19 | used_index.append(rune_index) | ||
| 20 | recipe_index += 1 | 20 | recipe_index += 1 | ||
| 21 | 21 | ||||
| 22 | if recipe_index != len(recipe): | 22 | if recipe_index != len(recipe): | ||
| 23 | return False | 23 | return False | ||
| 24 | 24 | ||||
| 25 | for index in reversed(used_index): | 25 | for index in reversed(used_index): | ||
| 26 | self.runes.pop(index) | 26 | self.runes.pop(index) | ||
| 27 | 27 | ||||
| 28 | return True | 28 | return True | ||
| 29 | 29 | ||||
| t | 30 | t | |||
| 31 | def __next__(self): | 30 | def __next__(self): | ||
| 32 | for name, recipe in self.runewords.items(): | 31 | for name, recipe in self.runewords.items(): | ||
| 33 | if name not in self.returned and self._make_recipe(recipe): | 32 | if name not in self.returned and self._make_recipe(recipe): | ||
| 34 | self.returned.add(name) | 33 | self.returned.add(name) | ||
| 35 | return name | 34 | return name | ||
| 36 | 35 | ||||
| 37 | if len(self.returned) == len(self.runewords): | 36 | if len(self.returned) == len(self.runewords): | ||
| 38 | raise StopIteration | 37 | raise StopIteration | ||
| 39 | 38 | ||||
| 40 | return None | 39 | return None |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||