Домашни > Работилница за отвари! > Решения > Решението на Адем Црънчалиев

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

2 точки общо

4 успешни теста
16 неуспешни теста
Код
Скрий всички коментари

  1class Potion:
  2    def __init__(self, effects, duration):
  3        self.effects = effects
  4        self.duration = duration
  5        self.intensity = 1
  6
  7    def __eq__(self, other):
  8        return self.effects == other.effects and self.duration == other.duration
  9
 10    def __lt__(self, other):
 11        return self.duration < other.duration
 12
 13    def __gt__(self, other):
 14        return self.duration > other.duration
 15
 16    def __call__(self, target):
 17        for _ in range(self.intensity):
 18            for effect in self.effects:
 19                self.effects[effect](target)
 20
 21    def __add__(self, other):
 22        combined_effects = {**self.effects, **other.effects}
 23        combined_duration = max(self.duration, other.duration)
 24
 25        return Potion(combined_effects, combined_duration)
 26
 27    def __mul__(self, intensity):
 28        new_potion = Potion(self.effects, self.duration)
 29        new_potion.intensity = self.intensity * intensity
 30
 31        return new_potion
 32    
 33    def __sub__(self, other):
 34        subtracted_effects = {
 35            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)
 36            for effect in self.effects
 37        }
 38
 39        subtracted_potion = Potion(subtracted_effects, self.duration)
 40        subtracted_potion.intensity = self.intensity
 41        return subtracted_potion
 42
 43    def __truediv__(self, dilution_factor):
 44        diluted_effects = {
 45            effect: lambda target, eff=self.effects[effect]: eff(target)
 46            for effect in self.effects
 47        }
 48        diluted_potion = Potion(diluted_effects, self.duration)
 49        diluted_potion.intensity = self.intensity * dilution_factor
 50        return diluted_potion
 51
 52    def apply_effect(self, effect, target):
 53        if effect in self.effects:
 54            self.effects[effect](target)
 55        else:
 56            raise AttributeError()
 57
 58    def __getattr__(self, name):
 59        if name in self.effects:
 60            return lambda target: self.apply_effect(name, target)
 61
 62        raise AttributeError()
 63
 64    def __init__(self, effects, duration):
 65        self.effects = effects
 66        self.duration = duration
 67        self.intensity = 1
 68
 69    def __eq__(self, other):
 70        return self.effects == other.effects and self.duration == other.duration
 71
 72    def __lt__(self, other):
 73        return self.duration < other.duration
 74
 75    def __gt__(self, other):
 76        return self.duration > other.duration
 77
 78    def __call__(self, target):
 79        for _ in range(self.intensity):
 80            for effect in self.effects:
 81                self.effects[effect](target)
 82
 83    def __add__(self, other):
 84        combined_effects = {**self.effects, **other.effects}
 85        combined_duration = max(self.duration, other.duration)
 86
 87        return Potion(combined_effects, combined_duration)
 88
 89    def __mul__(self, intensity):
 90        new_potion = Potion(self.effects, self.duration)
 91        new_potion.intensity = self.intensity * intensity
 92
 93        return new_potion
 94    
 95    def __sub__(self, other):
 96        subtracted_effects = {
 97            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)
 98            for effect in self.effects
 99        }
100
101        subtracted_potion = Potion(subtracted_effects, self.duration)
102        subtracted_potion.intensity = self.intensity
103        return subtracted_potion
104
105    def __truediv__(self, dilution_factor):
106        diluted_effects = {
107            effect: lambda target, eff=self.effects[effect]: eff(target)
108            for effect in self.effects
109        }
110        diluted_potion = Potion(diluted_effects, self.duration)
111        diluted_potion.intensity = self.intensity * dilution_factor
112        return diluted_potion
113
114    def apply_effect(self, effect, target):
115        if effect in self.effects:
116            self.effects[effect](target)
117        else:
118            raise AttributeError()
119
120    def __getattr__(self, name):
121        if name in self.effects:
122            return lambda target: self.apply_effect(name, target)
123
124        raise AttributeError()
125
126class ГоспожатаПоХимия:
127    def __init__(self):
128        self.active_potions = []
129        self.tick_count = 0
130
131    def apply(self, target, potion):
132        if potion in self.active_potions:
133            raise TypeError("Potion is depleted.")
134
135        self.active_potions.append(potion)
136        potion(target)
137
138
139    def tick(self):
140        self.tick_count += 1
141
142        expired_potions = [potion for potion in self.active_potions if potion.duration <= self.tick_count]
143        
144        for potion in expired_potions:
145            self.active_potions.remove(potion)

.F.FF.FFFFFEF.FFFFFF
======================================================================
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
TypeError: cannot unpack non-iterable Potion object

======================================================================
FAIL: test_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 78, in test_depletion
with self.assertRaisesRegex(TypeError, 'Effect is depleted\.'):
AssertionError: TypeError not raised

======================================================================
FAIL: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 310, in test_equal
self.assertNotEqual(potion4, potion3)
AssertionError: <solution.Potion object at 0x7f551b36b8e0> == <solution.Potion object at 0x7f551b36a890>

======================================================================
FAIL: 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)
AssertionError: <solution.Potion object at 0x7f551b36b8e0> not less than <solution.Potion object at 0x7f551b369480>

======================================================================
FAIL: test_combination_with_overlap (test.TestPotionOperations)
Test combining potions with overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 105, in test_combination_with_overlap
self.assertEqual(self._target.int_attr, 500)
AssertionError: 50 != 500

======================================================================
FAIL: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 257, in test_deprecation
with self.assertRaisesRegex(TypeError, 'Potion is now part of something bigger than itself\.'):
AssertionError: TypeError not raised

======================================================================
FAIL: test_dilution (test.TestPotionOperations)
Test dilution of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 120, in test_dilution
self.assertEqual(self._target.int_attr, 5)
AssertionError: 50 != 5

======================================================================
FAIL: test_potentiation (test.TestPotionOperations)
Test potentiation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 112, in test_potentiation
self.assertEqual(self._target.int_attr, 5 * (10 ** 3))
AssertionError: 50 != 5000

======================================================================
FAIL: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 168, in test_purification
with self.assertRaises(AttributeError):
AssertionError: AttributeError not raised

======================================================================
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_applying_part_of_potion (test.TestГоспожатаПоХимия)
Test applying only a part of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 366, in test_applying_part_of_potion
self.assertEqual(self._target.int_attr, 5) # This should be the original value
AssertionError: 50 != 5

======================================================================
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_multiple_targets (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 490, in test_ticking_multiple_targets
self.assertEqual(target1.int_attr, 5)
AssertionError: 500 != 5

======================================================================
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=15, errors=1)

Дискусия
Адем Црънчалиев
05.12.2023 16:29

Реших да ви изпробвам :D
Виктор Бечев
05.12.2023 14:35

Looks good to me. Pull request approved.
История

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.intensity = 15        self.intensity = 1
66
7    def __eq__(self, other):7    def __eq__(self, other):
8        return self.effects == other.effects and self.duration == other.duration8        return self.effects == other.effects and self.duration == other.duration
99
10    def __lt__(self, other):10    def __lt__(self, other):
11        return self.duration < other.duration11        return self.duration < other.duration
1212
13    def __gt__(self, other):13    def __gt__(self, other):
14        return self.duration > other.duration14        return self.duration > other.duration
1515
16    def __call__(self, target):16    def __call__(self, target):
17        for _ in range(self.intensity):17        for _ in range(self.intensity):
18            for effect in self.effects:18            for effect in self.effects:
19                self.effects[effect](target)19                self.effects[effect](target)
2020
21    def __add__(self, other):21    def __add__(self, other):
22        combined_effects = {**self.effects, **other.effects}22        combined_effects = {**self.effects, **other.effects}
23        combined_duration = max(self.duration, other.duration)23        combined_duration = max(self.duration, other.duration)
n24        n24 
25        return Potion(combined_effects, combined_duration)25        return Potion(combined_effects, combined_duration)
2626
27    def __mul__(self, intensity):27    def __mul__(self, intensity):
28        new_potion = Potion(self.effects, self.duration)28        new_potion = Potion(self.effects, self.duration)
29        new_potion.intensity = self.intensity * intensity29        new_potion.intensity = self.intensity * intensity
3030
31        return new_potion31        return new_potion
32    32    
33    def __sub__(self, other):33    def __sub__(self, other):
34        subtracted_effects = {34        subtracted_effects = {
35            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)35            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)
36            for effect in self.effects36            for effect in self.effects
37        }37        }
3838
39        subtracted_potion = Potion(subtracted_effects, self.duration)39        subtracted_potion = Potion(subtracted_effects, self.duration)
40        subtracted_potion.intensity = self.intensity40        subtracted_potion.intensity = self.intensity
41        return subtracted_potion41        return subtracted_potion
4242
43    def __truediv__(self, dilution_factor):43    def __truediv__(self, dilution_factor):
44        diluted_effects = {44        diluted_effects = {
45            effect: lambda target, eff=self.effects[effect]: eff(target)45            effect: lambda target, eff=self.effects[effect]: eff(target)
46            for effect in self.effects46            for effect in self.effects
47        }47        }
48        diluted_potion = Potion(diluted_effects, self.duration)48        diluted_potion = Potion(diluted_effects, self.duration)
49        diluted_potion.intensity = self.intensity * dilution_factor49        diluted_potion.intensity = self.intensity * dilution_factor
50        return diluted_potion50        return diluted_potion
5151
52    def apply_effect(self, effect, target):52    def apply_effect(self, effect, target):
53        if effect in self.effects:53        if effect in self.effects:
54            self.effects[effect](target)54            self.effects[effect](target)
55        else:55        else:
56            raise AttributeError()56            raise AttributeError()
5757
58    def __getattr__(self, name):58    def __getattr__(self, name):
59        if name in self.effects:59        if name in self.effects:
60            return lambda target: self.apply_effect(name, target)60            return lambda target: self.apply_effect(name, target)
6161
62        raise AttributeError()62        raise AttributeError()
tt63 
64    def __init__(self, effects, duration):
65        self.effects = effects
66        self.duration = duration
67        self.intensity = 1
68 
69    def __eq__(self, other):
70        return self.effects == other.effects and self.duration == other.duration
71 
72    def __lt__(self, other):
73        return self.duration < other.duration
74 
75    def __gt__(self, other):
76        return self.duration > other.duration
77 
78    def __call__(self, target):
79        for _ in range(self.intensity):
80            for effect in self.effects:
81                self.effects[effect](target)
82 
83    def __add__(self, other):
84        combined_effects = {**self.effects, **other.effects}
85        combined_duration = max(self.duration, other.duration)
86 
87        return Potion(combined_effects, combined_duration)
88 
89    def __mul__(self, intensity):
90        new_potion = Potion(self.effects, self.duration)
91        new_potion.intensity = self.intensity * intensity
92 
93        return new_potion
94    
95    def __sub__(self, other):
96        subtracted_effects = {
97            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)
98            for effect in self.effects
99        }
100 
101        subtracted_potion = Potion(subtracted_effects, self.duration)
102        subtracted_potion.intensity = self.intensity
103        return subtracted_potion
104 
105    def __truediv__(self, dilution_factor):
106        diluted_effects = {
107            effect: lambda target, eff=self.effects[effect]: eff(target)
108            for effect in self.effects
109        }
110        diluted_potion = Potion(diluted_effects, self.duration)
111        diluted_potion.intensity = self.intensity * dilution_factor
112        return diluted_potion
113 
114    def apply_effect(self, effect, target):
115        if effect in self.effects:
116            self.effects[effect](target)
117        else:
118            raise AttributeError()
119 
120    def __getattr__(self, name):
121        if name in self.effects:
122            return lambda target: self.apply_effect(name, target)
123 
124        raise AttributeError()
125 
126class ГоспожатаПоХимия:
127    def __init__(self):
128        self.active_potions = []
129        self.tick_count = 0
130 
131    def apply(self, target, potion):
132        if potion in self.active_potions:
133            raise TypeError("Potion is depleted.")
134 
135        self.active_potions.append(potion)
136        potion(target)
137 
138 
139    def tick(self):
140        self.tick_count += 1
141 
142        expired_potions = [potion for potion in self.active_potions if potion.duration <= self.tick_count]
143        
144        for potion in expired_potions:
145            self.active_potions.remove(potion)
146 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1print("")t1class Potion:
2    def __init__(self, effects, duration):
3        self.effects = effects
4        self.duration = duration
5        self.intensity = 1
6 
7    def __eq__(self, other):
8        return self.effects == other.effects and self.duration == other.duration
9 
10    def __lt__(self, other):
11        return self.duration < other.duration
12 
13    def __gt__(self, other):
14        return self.duration > other.duration
15 
16    def __call__(self, target):
17        for _ in range(self.intensity):
18            for effect in self.effects:
19                self.effects[effect](target)
20 
21    def __add__(self, other):
22        combined_effects = {**self.effects, **other.effects}
23        combined_duration = max(self.duration, other.duration)
24        
25        return Potion(combined_effects, combined_duration)
26 
27    def __mul__(self, intensity):
28        new_potion = Potion(self.effects, self.duration)
29        new_potion.intensity = self.intensity * intensity
30 
31        return new_potion
32    
33    def __sub__(self, other):
34        subtracted_effects = {
35            effect: lambda target, eff=self.effects[effect], intensity=other.intensity: max(0, eff(target) or 0 - intensity)
36            for effect in self.effects
37        }
38 
39        subtracted_potion = Potion(subtracted_effects, self.duration)
40        subtracted_potion.intensity = self.intensity
41        return subtracted_potion
42 
43    def __truediv__(self, dilution_factor):
44        diluted_effects = {
45            effect: lambda target, eff=self.effects[effect]: eff(target)
46            for effect in self.effects
47        }
48        diluted_potion = Potion(diluted_effects, self.duration)
49        diluted_potion.intensity = self.intensity * dilution_factor
50        return diluted_potion
51 
52    def apply_effect(self, effect, target):
53        if effect in self.effects:
54            self.effects[effect](target)
55        else:
56            raise AttributeError()
57 
58    def __getattr__(self, name):
59        if name in self.effects:
60            return lambda target: self.apply_effect(name, target)
61 
62        raise AttributeError()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op