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

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

1 точки общо

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

 1class RunewordsCalculator:
 2    def __init__(self, runewords: dict) -> None:
 3        self.recipes = [[name, recipe, False] for name, recipe in runewords.items()]
 4        self.available_runes = []
 5
 6    def add_runes(self, runes: list) -> None:
 7        self.available_runes.extend(runes)
 8
 9    def __iter__(self):
10        return self
11
12    def _can_build(self, recipe: tuple) -> bool:
13        it = iter(self.available_runes)
14        return all(rune in it for rune in recipe)
15
16    def __next__(self) -> str | None:
17        remaining_recipes = [r for r in self.recipes if not r[2]]
18        
19        if not remaining_recipes:
20            raise StopIteration
21
22        for recipe_data in self.recipes:
23            name, recipe, issued = recipe_data
24            
25            if not issued:
26                if self._can_build(recipe):
27                    recipe_data[2] = True
28                    return name
29        
30        return None

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

OK

Дискусия
Виктор Бечев
21.04.2026 08:33

Виж корекцията на условието, защото не сме били достатъчно ясни в намерението си за един дребен детайл от поведението на итератора.
История

f1class RunewordsCalculator:f1class RunewordsCalculator:
n2    n
3    def __init__(self, runewords: dict) -> None:2    def __init__(self, runewords: dict) -> None:
n4        self.recipies = list(runewords.items())n3        self.recipes = [[name, recipe, False] for name, recipe in runewords.items()]
5        self.available_runes = []4        self.available_runes = []
n6        self.current_index = 0n
75
8    def add_runes(self, runes: list) -> None:6    def add_runes(self, runes: list) -> None:
9        self.available_runes.extend(runes)7        self.available_runes.extend(runes)
108
11    def __iter__(self):9    def __iter__(self):
12        return self10        return self
1311
nn12    def _can_build(self, recipe: tuple) -> bool:
13        it = iter(self.available_runes)
14        return all(rune in it for rune in recipe)
15 
14    def __next__(self) -> str | None:16    def __next__(self) -> str | None:
nn17        remaining_recipes = [r for r in self.recipes if not r[2]]
15        18        
n16        if self.current_index >= len(self.recipies):n19        if not remaining_recipes:
17            raise StopIteration20            raise StopIteration
1821
n19        name, recipie = self.recipies[self.current_index]n22        for recipe_data in self.recipes:
20        it = iter(self.available_runes)23            name, recipe, issued = recipe_data
21        can_build = all(rune in it for rune in recipie)24            
22 25            if not issued:
23        if can_build:26                if self._can_build(recipe):
24            self.current_index += 127                    recipe_data[2] = True
25            return name28                    return name
29        
26        return None30        return None
t27 t
28 
29#calculator = RunewordsCalculator({
30#    "Enigma": ("Ber", "Ith", "Eld")
31#})
32 
33#print(next(iter(calculator)))  # None
34#calculator.add_runes(["Ber", "Ith", "Eld"])
35#print(next(iter(calculator)))  # Enigma
36#print(next(iter(calculator)))  # Хвърля StopIteration
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op