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

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

1 точки общо

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

 1from collections import Counter
 2
 3class RunewordsCalculator:
 4    def __init__(self, runewords : dict[str, list | tuple [str]]):
 5        self.__runewords_recepies = runewords
 6        self.__runes = []
 7        self.__used = set()
 8
 9    def add_runes(self, runes : tuple | list [str]) -> None:
10        self.__runes.extend(runes)
11
12    def __use_runes(self, used_runes :  iter[str]) -> None:
13        new_runes = []
14        used_rune = next(used_runes)
15        for founded_rune in self.__runes:
16            if used_rune == founded_rune:
17                try:
18                    used_rune = next(used_runes)
19                except StopIteration:
20                    used_rune = None
21                    continue
22            else:
23                new_runes.append(founded_rune)
24        self.__runes = new_runes
25        return None
26
27
28    def __cooking_runewords(self, recipe : iter[str]) -> bool:
29        try:
30            needed_rune = next(recipe)
31            for founded_rune in self.__runes:
32                if needed_rune == founded_rune:
33                    needed_rune = next(recipe)
34            return False
35        except StopIteration:
36            return True
37
38    def __iter__(self):
39        return self
40
41    def __next__(self):
42        if all(runeword in self.__used for runeword in self.__runewords_recepies.keys()):
43            raise StopIteration
44
45        for runeword, recipe in self.__runewords_recepies.items():
46            if runeword in self.__used:
47                continue
48            elif self.__cooking_runewords((rune for rune in recipe)):
49                self.__use_runes((rune for rune in recipe))
50                self.__used.add(runeword)
51                return runeword
52        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)

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

f1from collections import Counterf1from collections import Counter
22
3class RunewordsCalculator:3class RunewordsCalculator:
4    def __init__(self, runewords : dict[str, list | tuple [str]]):4    def __init__(self, runewords : dict[str, list | tuple [str]]):
5        self.__runewords_recepies = runewords5        self.__runewords_recepies = runewords
6        self.__runes = []6        self.__runes = []
7        self.__used = set()7        self.__used = set()
88
9    def add_runes(self, runes : tuple | list [str]) -> None:9    def add_runes(self, runes : tuple | list [str]) -> None:
10        self.__runes.extend(runes)10        self.__runes.extend(runes)
1111
12    def __use_runes(self, used_runes :  iter[str]) -> None:12    def __use_runes(self, used_runes :  iter[str]) -> None:
13        new_runes = []13        new_runes = []
14        used_rune = next(used_runes)14        used_rune = next(used_runes)
15        for founded_rune in self.__runes:15        for founded_rune in self.__runes:
16            if used_rune == founded_rune:16            if used_rune == founded_rune:
17                try:17                try:
18                    used_rune = next(used_runes)18                    used_rune = next(used_runes)
19                except StopIteration:19                except StopIteration:
20                    used_rune = None20                    used_rune = None
21                    continue21                    continue
22            else:22            else:
23                new_runes.append(founded_rune)23                new_runes.append(founded_rune)
24        self.__runes = new_runes24        self.__runes = new_runes
25        return None25        return None
2626
2727
28    def __cooking_runewords(self, recipe : iter[str]) -> bool:28    def __cooking_runewords(self, recipe : iter[str]) -> bool:
29        try:29        try:
30            needed_rune = next(recipe)30            needed_rune = next(recipe)
31            for founded_rune in self.__runes:31            for founded_rune in self.__runes:
32                if needed_rune == founded_rune:32                if needed_rune == founded_rune:
33                    needed_rune = next(recipe)33                    needed_rune = next(recipe)
34            return False34            return False
35        except StopIteration:35        except StopIteration:
36            return True36            return True
3737
38    def __iter__(self):38    def __iter__(self):
39        return self39        return self
4040
41    def __next__(self):41    def __next__(self):
42        if all(runeword in self.__used for runeword in self.__runewords_recepies.keys()):42        if all(runeword in self.__used for runeword in self.__runewords_recepies.keys()):
43            raise StopIteration43            raise StopIteration
4444
45        for runeword, recipe in self.__runewords_recepies.items():45        for runeword, recipe in self.__runewords_recepies.items():
46            if runeword in self.__used:46            if runeword in self.__used:
47                continue47                continue
48            elif self.__cooking_runewords((rune for rune in recipe)):48            elif self.__cooking_runewords((rune for rune in recipe)):
49                self.__use_runes((rune for rune in recipe))49                self.__use_runes((rune for rune in recipe))
50                self.__used.add(runeword)50                self.__used.add(runeword)
51                return runeword51                return runeword
52        return None52        return None
t53    t
54 
55calculator = RunewordsCalculator({
56    "X": ("A", "A")
57})
58 
59calculator.add_runes(["A", "B", "A"])
60 
61print(next(iter(calculator)))  # X  (ползва A, B)
62print(next(iter(calculator)))  # None (остава само C)
63 
64calculator.add_runes(["A", "A"])
65 
66print(next(iter(calculator)))  # Y или Z (зависи от реда)
67print(calculator._RunewordsCalculator__runes)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op