1class Potion:
2 def __init__(self, effects, duration=1):
3 self.effects = effects
4 self.duration = duration
5 self.used_potion = False
6
7 def apply_effects(self, target, intensity=1):
8 if self.used_potion:
9 raise TypeError("Potion is now part of something bigger than itself.")
10 for effect_name, effect_func in self.effects.items():
11 for _ in range(intensity):
12 effect_func(target)
13 self.used_potion = True
14
15 def __add__(self, other):
16 combined_effects = {}
17 combined_effects.update(self.effects)
18 combined_effects.update(other.effects)
19 combined_duration = max(self.duration, other.duration)
20 return Potion(combined_effects, combined_duration)
21
22 def __mul__(self, intensity):
23 return Potion(self.effects, self.duration * intensity)
24
25 def __truediv__(self, divisor):
26 intensity = 1 / divisor
27 return Potion(self.effects, self.duration).apply_effects(target, intensity)
28
29 def __sub__(self, other):
30 if any(effect_name not in self.effects for effect_name in other.effects):
31 raise TypeError("Cannot subtract potion with different effects.")
32 sub_effects = {name: max(0, self.effects[name] - other.effects[name]) for name in self.effects}
33 sub_duration = self.duration
34 return Potion(sub_effects, sub_duration)
35
36 def __eq__(self, other):
37 return self.effects == other.effects and self.duration == other.duration
38
39 def __lt__(self, other):
40 return sum(self.effects.values()) < sum(other.effects.values())
41
42 def __gt__(self, other):
43 return sum(self.effects.values()) > sum(other.effects.values())
44
45
46class ГоспожатаПоХимия:
47 def __init__(self):
48 self.active_potions = []
49 self.tick_count = 0
50
51 def apply(self, target, potion):
52 if potion.used_potion:
53 raise TypeError("Potion is depleted.")
54 if potion in self.active_potions:
55 raise TypeError("Potion is now part of something bigger than itself.")
56
57 sorted_effects = sorted(potion.effects.keys(), key=lambda x: sum(map(ord, x)))
58
59 for effect_name in sorted_effects:
60 potion.effects[effect_name](target)
61
62 potion.used_potion = True
63 self.active_potions.append(potion)
64
65 def tick(self):
66 self.tick_count += 1
67
68 self.active_potions = [potion for potion in self.active_potions if potion.duration > self.tick_count]
69
70 for potion in self.active_potions:
71 potion.used_potion = False
EE.FEEEEEEEEF.FEFFEF
======================================================================
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:
File "/tmp/solution.py", line 40, in __lt__
return sum(self.effects.values()) < sum(other.effects.values())
TypeError: unsupported operand type(s) for +: 'int' and 'function'
======================================================================
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 32, in __sub__
sub_effects = {name: max(0, self.effects[name] - other.effects[name]) for name in self.effects}
File "/tmp/solution.py", line 32, in <dictcomp>
sub_effects = {name: max(0, self.effects[name] - other.effects[name]) for name in self.effects}
TypeError: unsupported operand type(s) for -: 'function' and 'function'
======================================================================
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 27, in __truediv__
return Potion(self.effects, self.duration).apply_effects(target, intensity)
NameError: name 'target' 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 364, in test_applying_part_of_potion
potion.int_attr_fun(temp_target)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'
======================================================================
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 486, in test_ticking_multiple_targets
self._dimitrichka.apply(target2, potion2)
File "/tmp/solution.py", line 55, in apply
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.
======================================================================
FAIL: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 298, in test_equal
self.assertEqual(potion4, potion3)
AssertionError: <solution.Potion object at 0x7f1f9fd670d0> != <solution.Potion object at 0x7f1f9fd67010>
======================================================================
FAIL: 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 382, in test_applying_depleted_potion
with self.assertRaisesRegex(TypeError, 'Potion is depleted\.'):
AssertionError: TypeError not raised
======================================================================
FAIL: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 405, in test_applying_order
self.assertEqual(self._target.int_attr, 12)
AssertionError: 14 != 12
======================================================================
FAIL: test_ticking_immutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with immutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 425, in test_ticking_immutable
self.assertEqual(self._target.int_attr, 500)
AssertionError: 50 != 500
======================================================================
FAIL: 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 458, in test_ticking_multiple_potions
self.assertEqual(self._target.int_attr, 50)
AssertionError: 500 != 50
======================================================================
FAIL: test_ticking_mutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 444, in test_ticking_mutable
self.assertEqual(self._target.int_attr, 5)
AssertionError: 50 != 5
----------------------------------------------------------------------
Ran 20 tests in 0.002s
FAILED (failures=6, errors=12)
Виктор Бечев
04.12.2023 14:20По отношение на стил и изчистеност на кода - нямам забележки. :)
|