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Виж корекцията на условието, защото не сме били достатъчно ясни в намерението си за един дребен детайл от поведението на итератора.
|
| f | 1 | class RunewordsCalculator: | f | 1 | class RunewordsCalculator: |
| n | 2 | n | |||
| 3 | def __init__(self, runewords: dict) -> None: | 2 | def __init__(self, runewords: dict) -> None: | ||
| n | 4 | self.recipies = list(runewords.items()) | n | 3 | self.recipes = [[name, recipe, False] for name, recipe in runewords.items()] |
| 5 | self.available_runes = [] | 4 | self.available_runes = [] | ||
| n | 6 | self.current_index = 0 | n | ||
| 7 | 5 | ||||
| 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) | ||
| 10 | 8 | ||||
| 11 | def __iter__(self): | 9 | def __iter__(self): | ||
| 12 | return self | 10 | return self | ||
| 13 | 11 | ||||
| n | n | 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 | |||||
| 14 | def __next__(self) -> str | None: | 16 | def __next__(self) -> str | None: | ||
| n | n | 17 | remaining_recipes = [r for r in self.recipes if not r[2]] | ||
| 15 | 18 | ||||
| n | 16 | if self.current_index >= len(self.recipies): | n | 19 | if not remaining_recipes: |
| 17 | raise StopIteration | 20 | raise StopIteration | ||
| 18 | 21 | ||||
| n | 19 | name, recipie = self.recipies[self.current_index] | n | 22 | 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 += 1 | 27 | recipe_data[2] = True | ||
| 25 | return name | 28 | return name | ||
| 29 | |||||
| 26 | return None | 30 | return None | ||
| t | 27 | 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||