Домашни > Работилница за отвари! > Решения > Решението на Мартин Гоцев

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

0 точки общо

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

 1class Potion:
 2    def __init__(self, effects, duration):
 3        self.effects = effects
 4        self.duration = duration
 5        self.used_effects = {effect: False for effect in effects}
 6
 7    def __add__(self, other):
 8        combined_effects = self.effects.copy()
 9        combined_effects.update(other.effects)
10        combined_duration = max(self.duration, other.duration)
11        return Potion(combined_effects, combined_duration)
12
13    def __mul__(self, factor):
14        multiplied_effects = {key: self.effects[key] for key in self.effects}
15        multiplied_duration = self.duration
16        return Potion(multiplied_effects, multiplied_duration * factor)
17
18    def __truediv__(self, divisor):
19        diluted_effects = {key: self.effects[key] for key in self.effects}
20        diluted_duration = self.duration
21        diluted_intensity = 1 / divisor
22        diluted_effects = {key: diluted_effects[key] for key in diluted_effects if diluted_effects[key] * diluted_intensity >= 1}
23        return Potion(diluted_effects, diluted_duration)
24
25    def __sub__(self, other):
26        if not all(effect in other.effects for effect in self.effects):
27            raise TypeError("Content mismatch")
28        subtracted_effects = {key: self.effects[key] - other.effects.get(key, 0) for key in self.effects}
29        subtracted_duration = self.duration
30        subtracted_effects = {key: value for key, value in subtracted_effects.items() if value > 0}
31        return Potion(subtracted_effects, subtracted_duration)
32
33    def __floordiv__(self, divisor):
34        divided_effects = {key: self.effects[key] for key in self.effects}
35        divided_duration = self.duration
36        divided_intensity = 1 / divisor
37        divided_effects = {key: divided_effects[key] * divided_intensity for key in divided_effects}
38        return [Potion({key: value for key, value in divided_effects.items() if value > 0}, divided_duration)] * divisor
39    
40    def apply_effect(self, effect_name, target):
41        if self.used_effects[effect_name]:
42            raise TypeError("Effect is depleted.")
43        
44        if effect_name in self.effects:
45            self.effects[effect_name](target)
46            self.used_effects[effect_name] = True
47
48    def is_used_up(self):
49        return all(self.used_effects.values())
50    
51    class ГоспожатаПоХимия:
52        def apply(self, target, potion):
53            if potion.is_used_up():
54                raise TypeError("Potion is depleted.")
55
56            effects_sorted = sorted(potion.effects.keys(), key=lambda x: sum(ord(c) for c in x), reverse=True)
57            
58            for effect_name in effects_sorted:
59                potion.apply_effect(effect_name, target)
60
61        def tick(self):
62            self.ticks_passed += 1
63            if self.ticks_passed >= self.duration:
64                for effect_name in self.used_effects:
65                    self.used_effects[effect_name] = False
66                self.ticks_passed = 0

EE.FEEEEEEEEEEEEEEEE
======================================================================
ERROR: test_applying (test.TestBasicPotion)
Test applying a potion to a target.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 63, in test_applying
potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 76, in test_depletion
potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_superbness (test.TestPotionComparison)
Test superbness of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 317, in test_superbness
self.assertLess(potion1, potion2)
File "/usr/lib/python3.10/unittest/case.py", line 1230, in assertLess
if not a < b:
TypeError: '<' not supported between instances of 'Potion' and 'Potion'

======================================================================
ERROR: test_combination_no_overlap (test.TestPotionOperations)
Test combining potions with no overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 94, in test_combination_no_overlap
potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_combination_with_overlap (test.TestPotionOperations)
Test combining potions with overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 104, in test_combination_with_overlap
potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 258, in test_deprecation
potion1.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_dilution (test.TestPotionOperations)
Test dilution of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 119, in test_dilution
half_potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_potentiation (test.TestPotionOperations)
Test potentiation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 111, in test_potentiation
potion.int_attr_fun(self._target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
ERROR: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 165, in test_purification
potion = potion1 - potion2
File "/tmp/solution.py", line 27, in __sub__
raise TypeError("Content mismatch")
TypeError: Content mismatch

======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 213, in test_separation
potion1, potion2, potion3 = potion / 3
File "/tmp/solution.py", line 22, in __truediv__
diluted_effects = {key: diluted_effects[key] for key in diluted_effects if diluted_effects[key] * diluted_intensity >= 1}
File "/tmp/solution.py", line 22, in <dictcomp>
diluted_effects = {key: diluted_effects[key] for key in diluted_effects if diluted_effects[key] * diluted_intensity >= 1}
TypeError: unsupported operand type(s) for *: 'function' and 'float'

======================================================================
ERROR: test_applying_depleted_potion (test.TestГоспожатаПоХимия)
Test applying a depleted potion or a potion that was used in a reaction.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_normal_case (test.TestГоспожатаПоХимия)
Test applying a normal potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_part_of_potion (test.TestГоспожатаПоХимия)
Test applying only a part of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_ticking_immutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with immutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_ticking_multiple_potions (test.TestГоспожатаПоХимия)
Test ticking after applying multiple potions which affect the same attribute.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_ticking_multiple_targets (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_ticking_mutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
FAIL: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 292, in test_equal
self.assertEqual(potion1 + potion2, potion3)
AssertionError: <solution.Potion object at 0x7f212e0e7100> != <solution.Potion object at 0x7f212e0e70d0>

----------------------------------------------------------------------
Ran 20 tests in 0.002s

FAILED (failures=1, errors=18)

Дискусия
Виктор Бечев
06.12.2023 09:48

Кодът е чист и подреден, но уви - недостатъчно.
История

f1class Potion:f1class Potion:
2    def __init__(self, effects, duration):2    def __init__(self, effects, duration):
3        self.effects = effects3        self.effects = effects
4        self.duration = duration4        self.duration = duration
5        self.used_effects = {effect: False for effect in effects}5        self.used_effects = {effect: False for effect in effects}
66
7    def __add__(self, other):7    def __add__(self, other):
8        combined_effects = self.effects.copy()8        combined_effects = self.effects.copy()
9        combined_effects.update(other.effects)9        combined_effects.update(other.effects)
10        combined_duration = max(self.duration, other.duration)10        combined_duration = max(self.duration, other.duration)
11        return Potion(combined_effects, combined_duration)11        return Potion(combined_effects, combined_duration)
1212
13    def __mul__(self, factor):13    def __mul__(self, factor):
14        multiplied_effects = {key: self.effects[key] for key in self.effects}14        multiplied_effects = {key: self.effects[key] for key in self.effects}
15        multiplied_duration = self.duration15        multiplied_duration = self.duration
16        return Potion(multiplied_effects, multiplied_duration * factor)16        return Potion(multiplied_effects, multiplied_duration * factor)
1717
18    def __truediv__(self, divisor):18    def __truediv__(self, divisor):
19        diluted_effects = {key: self.effects[key] for key in self.effects}19        diluted_effects = {key: self.effects[key] for key in self.effects}
20        diluted_duration = self.duration20        diluted_duration = self.duration
21        diluted_intensity = 1 / divisor21        diluted_intensity = 1 / divisor
22        diluted_effects = {key: diluted_effects[key] for key in diluted_effects if diluted_effects[key] * diluted_intensity >= 1}22        diluted_effects = {key: diluted_effects[key] for key in diluted_effects if diluted_effects[key] * diluted_intensity >= 1}
23        return Potion(diluted_effects, diluted_duration)23        return Potion(diluted_effects, diluted_duration)
2424
25    def __sub__(self, other):25    def __sub__(self, other):
26        if not all(effect in other.effects for effect in self.effects):26        if not all(effect in other.effects for effect in self.effects):
27            raise TypeError("Content mismatch")27            raise TypeError("Content mismatch")
28        subtracted_effects = {key: self.effects[key] - other.effects.get(key, 0) for key in self.effects}28        subtracted_effects = {key: self.effects[key] - other.effects.get(key, 0) for key in self.effects}
29        subtracted_duration = self.duration29        subtracted_duration = self.duration
30        subtracted_effects = {key: value for key, value in subtracted_effects.items() if value > 0}30        subtracted_effects = {key: value for key, value in subtracted_effects.items() if value > 0}
31        return Potion(subtracted_effects, subtracted_duration)31        return Potion(subtracted_effects, subtracted_duration)
3232
33    def __floordiv__(self, divisor):33    def __floordiv__(self, divisor):
34        divided_effects = {key: self.effects[key] for key in self.effects}34        divided_effects = {key: self.effects[key] for key in self.effects}
35        divided_duration = self.duration35        divided_duration = self.duration
36        divided_intensity = 1 / divisor36        divided_intensity = 1 / divisor
37        divided_effects = {key: divided_effects[key] * divided_intensity for key in divided_effects}37        divided_effects = {key: divided_effects[key] * divided_intensity for key in divided_effects}
38        return [Potion({key: value for key, value in divided_effects.items() if value > 0}, divided_duration)] * divisor38        return [Potion({key: value for key, value in divided_effects.items() if value > 0}, divided_duration)] * divisor
39    39    
40    def apply_effect(self, effect_name, target):40    def apply_effect(self, effect_name, target):
41        if self.used_effects[effect_name]:41        if self.used_effects[effect_name]:
42            raise TypeError("Effect is depleted.")42            raise TypeError("Effect is depleted.")
43        43        
44        if effect_name in self.effects:44        if effect_name in self.effects:
45            self.effects[effect_name](target)45            self.effects[effect_name](target)
46            self.used_effects[effect_name] = True46            self.used_effects[effect_name] = True
4747
48    def is_used_up(self):48    def is_used_up(self):
49        return all(self.used_effects.values())49        return all(self.used_effects.values())
50    50    
t51    class MadameChemistry:t51    class ГоспожатаПоХимия:
52        def apply(self, target, potion):52        def apply(self, target, potion):
53            if potion.is_used_up():53            if potion.is_used_up():
54                raise TypeError("Potion is depleted.")54                raise TypeError("Potion is depleted.")
5555
56            effects_sorted = sorted(potion.effects.keys(), key=lambda x: sum(ord(c) for c in x), reverse=True)56            effects_sorted = sorted(potion.effects.keys(), key=lambda x: sum(ord(c) for c in x), reverse=True)
57            57            
58            for effect_name in effects_sorted:58            for effect_name in effects_sorted:
59                potion.apply_effect(effect_name, target)59                potion.apply_effect(effect_name, target)
6060
61        def tick(self):61        def tick(self):
62            self.ticks_passed += 162            self.ticks_passed += 1
63            if self.ticks_passed >= self.duration:63            if self.ticks_passed >= self.duration:
64                for effect_name in self.used_effects:64                for effect_name in self.used_effects:
65                    self.used_effects[effect_name] = False65                    self.used_effects[effect_name] = False
66                self.ticks_passed = 066                self.ticks_passed = 0
6767
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op