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

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

1 точки общо

7 успешни теста
0 неуспешни теста
Код

 1class RunewordsCalculator:
 2    def __init__(self, runewords):
 3        self._recipes = runewords
 4        self._available_runes = []
 5        self._given_words = []
 6
 7    def add_runes(self, runes):
 8        self._available_runes.extend(runes)
 9
10    def __iter__(self):
11        return self
12
13    def __next__(self):
14        if len(self._given_words) == len(self._recipes):
15            raise StopIteration
16
17        for word_name, word_recipe in self._recipes.items():
18            if word_name not in self._given_words:
19                if self._check_if_possible(word_recipe):
20                    self._given_words.append(word_name)
21                    return word_name
22        
23        return None
24
25    def _check_if_possible(self, recipe):
26        temp_runes = self._available_runes[:]
27        
28        for rune in recipe:
29            try:
30                found_index = temp_runes.index(rune)
31                temp_runes = temp_runes[found_index + 1:]
32            except ValueError:
33                return False
34                
35        return True

.......
----------------------------------------------------------------------
Ran 7 tests in 0.000s

OK

Дискусия
История
Това решение има само една версия.