Предизвикателства > Runewords Calculator > Решения > Решението на Ирина Влайкова

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

6 успешни теста
1 неуспешни теста
Код

 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)

Дискусия
История

f1class RunewordsCalculator:f1class RunewordsCalculator:
2    def __init__(self, runewords):2    def __init__(self, runewords):
3        self.runewords = runewords3        self.runewords = runewords
4        self.runes = []4        self.runes = []
5        self.returned = set()5        self.returned = set()
66
7    def add_runes(self, runes):7    def add_runes(self, runes):
8        self.runes.extend(runes)8        self.runes.extend(runes)
99
10    def __iter__(self):10    def __iter__(self):
11        return self11        return self
1212
13    def _make_recipe(self, recipe):13    def _make_recipe(self, recipe):
14        used_index = []14        used_index = []
15        recipe_index = 015        recipe_index = 0
1616
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 += 120                recipe_index += 1
2121
22        if recipe_index != len(recipe):22        if recipe_index != len(recipe):
23            return False23            return False
2424
25        for index in reversed(used_index):25        for index in reversed(used_index):
26            self.runes.pop(index)26            self.runes.pop(index)
2727
28        return True28        return True
2929
t30 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 name34                return name
3635
37        if len(self.returned) == len(self.runewords):36        if len(self.returned) == len(self.runewords):
38            raise StopIteration37            raise StopIteration
3938
40        return None39        return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op