1import copy
2
3MIN_UNSIGNED_INT = 0
4
5class Potion:
6 def __init__(self, effects, duration):
7 self.effects = effects
8 self.duration = duration
9
10 self.effects_intensity = {}
11 self.create_intensity()
12
13 self.set_effects_as_methods()
14
15 self.is_unusable = False
16 self.usable_effects = set(self.effects.keys())
17
18 @staticmethod
19 def rounded(number):
20 rounded_num = round(number)
21 return rounded_num + 1 if number - rounded_num > 0.5 else rounded_num
22
23 def set_effects_as_methods(self):
24 for effect in self.effects:
25 setattr(self, effect, self.log_method_callings(effect, self.effects[effect]))
26 def check_potion_obsolete(self):
27 if self.is_unusable:
28 raise TypeError("Potion is now part of something bigger than itself.")
29
30 def log_method_callings(self, effect, method):
31 def wrapper(*args, **kwargs):
32 self.check_potion_obsolete()
33 if effect not in self.usable_effects:
34 raise TypeError(f"Effect is depleted.")
35
36 self.usable_effects.remove(effect)
37
38 for times in range(0, self.effects_intensity[effect]):
39 if self.effects_intensity[effect] == 1:
40 self.check_potion_obsolete()
41 return method(*args, **kwargs)
42 else:
43 self.check_potion_obsolete()
44 method(*args, **kwargs)
45
46 return method
47 return wrapper
48
49 def create_intensity(self):
50 self.effects_intensity = {effect:1 for effect in self.effects}
51
52 def __add__(self, other):
53 self.check_potion_obsolete()
54 new_effects = self.effects.copy()
55 new_intensity = self.effects_intensity.copy()
56 new_duration = self.duration
57 #effects
58 for effect in other.effects:
59 #if the same
60 if effect in self.effects:
61 new_intensity[effect] += 1
62 #if not the same
63 else:
64 new_effects[effect] = other.effects[effect]
65 new_intensity[effect] = 1
66 #duration
67 if self.duration < other.duration:
68 new_duration = other.duration
69
70 new_potion = Potion(new_effects, new_duration)
71 new_potion.effects_intensity = new_intensity
72 self.usable_effects.update(other.usable_effects)
73 new_potion.usable_effects = self.usable_effects
74
75 self.is_unusable = True
76
77 return new_potion
78
79 def __mul__(self, number):
80 self.check_potion_obsolete()
81 if number>0 and isinstance(number, int):
82 new_intensity = {intensity_effect: self.effects_intensity[intensity_effect] * number
83 for intensity_effect in self.effects_intensity}
84
85 new_potion = Potion(self.effects, self.duration)
86 new_potion.effects_intensity = new_intensity
87
88 self.is_unusable = True
89
90 return new_potion
91
92 elif 0 <= number <= 1 and isinstance(number, float):
93 new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] * number)
94 for intensity_effect in self.effects_intensity}
95
96 new_potion = Potion(self.effects, self.duration)
97 new_potion.effects_intensity = new_intensity
98
99 self.is_unusable = True
100
101 return new_potion
102
103 def __sub__(self, other):
104 self.check_potion_obsolete()
105 new_effects = self.effects.copy()
106 new_intensity = self.effects_intensity.copy()
107 for key in self.effects.keys():
108 print(key)
109 if key in other.effects.keys():
110 if self.effects_intensity[key] == 1:
111 del new_effects[key]
112 del new_intensity[key]
113 else:
114 new_intensity[key] -= other.effects_intensity[key]
115 if new_intensity[key] <= 0:
116 del new_effects[key]
117 del new_intensity[key]
118
119
120 new_potion = Potion(new_effects, self.duration)
121 new_potion.effects_intensity = new_intensity
122
123 self.is_unusable = True
124
125 return new_potion
126
127 def __truediv__(self, number):
128 self.check_potion_obsolete()
129 new_effects = self.effects.copy()
130
131 new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] / number)
132 for intensity_effect in self.effects_intensity}
133
134 new_potion = Potion(new_effects, self.duration)
135 new_potion.effects_intensity = new_intensity
136 new_potion.usable_effects = set(new_effects.keys())
137
138 new_potions = (new_potion, ) * number
139
140 self.is_unusable = True
141
142 return new_potions
143
144 def __eq__(self, other):
145 return self.effects == other.effects and self.effects_intensity == other.effects_intensity
146
147 def __gt__(self, other):
148 return sum(self.effects_intensity.values()) > sum(other.effects_intensity.values())
149
150 def __lt__(self, other):
151 return sum(self.effects_intensity.values()) < sum(other.effects_intensity.values())
152
153 def __getattr__(self, method_name):
154 return getattr(self, method_name)()
155class ГоспожатаПоХимия:
156 def __init__(self):
157 self.applied_potions = {}
158 self.given_target_copy = None
159 self.current_target_object = None
160
161 @staticmethod
162 def sum_of_unicode_chars(string):
163 return sum(map(ord, string))
164
165 def find_current_max_sum(self, names_of_effects):
166 max_sum = MIN_UNSIGNED_INT
167 max_sum_name = ""
168 for name in names_of_effects:
169 if self.sum_of_unicode_chars(name) > max_sum:
170 max_sum = self.sum_of_unicode_chars(name)
171 max_sum_name = name
172
173 return max_sum_name
174
175 def apply(self, target, potion):
176 names_of_effects = potion.usable_effects
177 self.applied_potions[tuple(names_of_effects)] = potion.duration
178 self.given_target_copy = copy.deepcopy(target)
179 self.current_target_object = target
180
181 if potion.is_unusable or len(potion.usable_effects) == 0:
182 raise TypeError("Potion is depleted.")
183 else:
184 potion.is_unusable = True
185
186 while len(potion.usable_effects) != 0:
187 current_max_sum_name_effect = self.find_current_max_sum(names_of_effects)
188 if current_max_sum_name_effect in potion.usable_effects:
189 potion.current_max_sum_name_effect(target)
190 else:
191 raise TypeError("Effect is depleted.")
192
193
194 def tick(self):
195 for potion in self.applied_potions:
196 self.applied_potions[potion] -= 1
197 if self.applied_potions[potion] == 0:
198 del self.applied_potions[potion]
199 self.current_target_object = self.given_target_copy
.......FF.E
Stdout:
int_attr_fun
float_attr_fun
EEEEEEEEE
======================================================================
ERROR: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 169, in test_purification
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded
Stdout:
int_attr_fun
float_attr_fun
======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 218, in test_separation
potion2.int_attr_fun(self._target)
File "/tmp/solution.py", line 34, in wrapper
raise TypeError(f"Effect is depleted.")
TypeError: Effect is depleted.
======================================================================
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 379, in test_applying_depleted_potion
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
ERROR: test_applying_normal_case (test.TestГоспожатаПоХимия)
Test applying a normal potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 350, in test_applying_normal_case
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
ERROR: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 404, in test_applying_order
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
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 365, in test_applying_part_of_potion
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
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 424, in test_ticking_immutable
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
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 454, in test_ticking_multiple_potions
self._dimitrichka.apply(self._target, potion1)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
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 485, in test_ticking_multiple_targets
self._dimitrichka.apply(target1, potion1)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
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 438, in test_ticking_mutable
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 189, in apply
potion.current_max_sum_name_effect(target)
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
File "/tmp/solution.py", line 154, in __getattr__
return getattr(self, method_name)()
[Previous line repeated 488 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
======================================================================
FAIL: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 259, 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 155, in test_dilution
self.assertEqual(self._target.int_attr, 50)
AssertionError: 500 != 50
----------------------------------------------------------------------
Ran 20 tests in 0.012s
FAILED (failures=2, errors=10)
| f | 1 | import copy | f | 1 | import copy |
| 2 | 2 | ||||
| 3 | MIN_UNSIGNED_INT = 0 | 3 | MIN_UNSIGNED_INT = 0 | ||
| 4 | 4 | ||||
| 5 | class Potion: | 5 | class Potion: | ||
| 6 | def __init__(self, effects, duration): | 6 | def __init__(self, effects, duration): | ||
| 7 | self.effects = effects | 7 | self.effects = effects | ||
| 8 | self.duration = duration | 8 | self.duration = duration | ||
| 9 | 9 | ||||
| 10 | self.effects_intensity = {} | 10 | self.effects_intensity = {} | ||
| 11 | self.create_intensity() | 11 | self.create_intensity() | ||
| 12 | 12 | ||||
| 13 | self.set_effects_as_methods() | 13 | self.set_effects_as_methods() | ||
| 14 | 14 | ||||
| 15 | self.is_unusable = False | 15 | self.is_unusable = False | ||
| 16 | self.usable_effects = set(self.effects.keys()) | 16 | self.usable_effects = set(self.effects.keys()) | ||
| 17 | 17 | ||||
| 18 | @staticmethod | 18 | @staticmethod | ||
| 19 | def rounded(number): | 19 | def rounded(number): | ||
| 20 | rounded_num = round(number) | 20 | rounded_num = round(number) | ||
| 21 | return rounded_num + 1 if number - rounded_num > 0.5 else rounded_num | 21 | return rounded_num + 1 if number - rounded_num > 0.5 else rounded_num | ||
| 22 | 22 | ||||
| 23 | def set_effects_as_methods(self): | 23 | def set_effects_as_methods(self): | ||
| 24 | for effect in self.effects: | 24 | for effect in self.effects: | ||
| 25 | setattr(self, effect, self.log_method_callings(effect, self.effects[effect])) | 25 | setattr(self, effect, self.log_method_callings(effect, self.effects[effect])) | ||
| 26 | def check_potion_obsolete(self): | 26 | def check_potion_obsolete(self): | ||
| 27 | if self.is_unusable: | 27 | if self.is_unusable: | ||
| 28 | raise TypeError("Potion is now part of something bigger than itself.") | 28 | raise TypeError("Potion is now part of something bigger than itself.") | ||
| 29 | 29 | ||||
| 30 | def log_method_callings(self, effect, method): | 30 | def log_method_callings(self, effect, method): | ||
| 31 | def wrapper(*args, **kwargs): | 31 | def wrapper(*args, **kwargs): | ||
| n | n | 32 | self.check_potion_obsolete() | ||
| 32 | if effect not in self.usable_effects: | 33 | if effect not in self.usable_effects: | ||
| n | 33 | raise TypeError("Effect is depleted.") | n | 34 | raise TypeError(f"Effect is depleted.") |
| 34 | 35 | ||||
| 35 | self.usable_effects.remove(effect) | 36 | self.usable_effects.remove(effect) | ||
| 36 | 37 | ||||
| 37 | for times in range(0, self.effects_intensity[effect]): | 38 | for times in range(0, self.effects_intensity[effect]): | ||
| 38 | if self.effects_intensity[effect] == 1: | 39 | if self.effects_intensity[effect] == 1: | ||
| n | n | 40 | self.check_potion_obsolete() | ||
| 39 | return method | 41 | return method(*args, **kwargs) | ||
| 40 | else: | 42 | else: | ||
| n | n | 43 | self.check_potion_obsolete() | ||
| 41 | method() | 44 | method(*args, **kwargs) | ||
| 42 | 45 | ||||
| 43 | return method | 46 | return method | ||
| 44 | return wrapper | 47 | return wrapper | ||
| 45 | 48 | ||||
| 46 | def create_intensity(self): | 49 | def create_intensity(self): | ||
| 47 | self.effects_intensity = {effect:1 for effect in self.effects} | 50 | self.effects_intensity = {effect:1 for effect in self.effects} | ||
| 48 | 51 | ||||
| 49 | def __add__(self, other): | 52 | def __add__(self, other): | ||
| 50 | self.check_potion_obsolete() | 53 | self.check_potion_obsolete() | ||
| 51 | new_effects = self.effects.copy() | 54 | new_effects = self.effects.copy() | ||
| 52 | new_intensity = self.effects_intensity.copy() | 55 | new_intensity = self.effects_intensity.copy() | ||
| 53 | new_duration = self.duration | 56 | new_duration = self.duration | ||
| 54 | #effects | 57 | #effects | ||
| n | 55 | for effect in self.effects: | n | 58 | for effect in other.effects: |
| 56 | #if the same | 59 | #if the same | ||
| n | 57 | if effect in other.effects: | n | 60 | if effect in self.effects: |
| 58 | new_intensity[effect] += 1 | 61 | new_intensity[effect] += 1 | ||
| 59 | #if not the same | 62 | #if not the same | ||
| 60 | else: | 63 | else: | ||
| 61 | new_effects[effect] = other.effects[effect] | 64 | new_effects[effect] = other.effects[effect] | ||
| 62 | new_intensity[effect] = 1 | 65 | new_intensity[effect] = 1 | ||
| 63 | #duration | 66 | #duration | ||
| 64 | if self.duration < other.duration: | 67 | if self.duration < other.duration: | ||
| 65 | new_duration = other.duration | 68 | new_duration = other.duration | ||
| 66 | 69 | ||||
| 67 | new_potion = Potion(new_effects, new_duration) | 70 | new_potion = Potion(new_effects, new_duration) | ||
| 68 | new_potion.effects_intensity = new_intensity | 71 | new_potion.effects_intensity = new_intensity | ||
| n | n | 72 | self.usable_effects.update(other.usable_effects) | ||
| 69 | new_potion.usable_effects = self.usable_effects | 73 | new_potion.usable_effects = self.usable_effects | ||
| 70 | 74 | ||||
| 71 | self.is_unusable = True | 75 | self.is_unusable = True | ||
| 72 | 76 | ||||
| 73 | return new_potion | 77 | return new_potion | ||
| 74 | 78 | ||||
| 75 | def __mul__(self, number): | 79 | def __mul__(self, number): | ||
| 76 | self.check_potion_obsolete() | 80 | self.check_potion_obsolete() | ||
| 77 | if number>0 and isinstance(number, int): | 81 | if number>0 and isinstance(number, int): | ||
| 78 | new_intensity = {intensity_effect: self.effects_intensity[intensity_effect] * number | 82 | new_intensity = {intensity_effect: self.effects_intensity[intensity_effect] * number | ||
| 79 | for intensity_effect in self.effects_intensity} | 83 | for intensity_effect in self.effects_intensity} | ||
| 80 | 84 | ||||
| 81 | new_potion = Potion(self.effects, self.duration) | 85 | new_potion = Potion(self.effects, self.duration) | ||
| 82 | new_potion.effects_intensity = new_intensity | 86 | new_potion.effects_intensity = new_intensity | ||
| 83 | 87 | ||||
| n | n | 88 | self.is_unusable = True | ||
| 89 | |||||
| 84 | return new_potion | 90 | return new_potion | ||
| 85 | 91 | ||||
| 86 | elif 0 <= number <= 1 and isinstance(number, float): | 92 | elif 0 <= number <= 1 and isinstance(number, float): | ||
| 87 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] * number) | 93 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] * number) | ||
| 88 | for intensity_effect in self.effects_intensity} | 94 | for intensity_effect in self.effects_intensity} | ||
| 89 | 95 | ||||
| 90 | new_potion = Potion(self.effects, self.duration) | 96 | new_potion = Potion(self.effects, self.duration) | ||
| 91 | new_potion.effects_intensity = new_intensity | 97 | new_potion.effects_intensity = new_intensity | ||
| 92 | 98 | ||||
| 93 | self.is_unusable = True | 99 | self.is_unusable = True | ||
| 94 | 100 | ||||
| 95 | return new_potion | 101 | return new_potion | ||
| 96 | 102 | ||||
| 97 | def __sub__(self, other): | 103 | def __sub__(self, other): | ||
| 98 | self.check_potion_obsolete() | 104 | self.check_potion_obsolete() | ||
| 99 | new_effects = self.effects.copy() | 105 | new_effects = self.effects.copy() | ||
| 100 | new_intensity = self.effects_intensity.copy() | 106 | new_intensity = self.effects_intensity.copy() | ||
| 101 | for key in self.effects.keys(): | 107 | for key in self.effects.keys(): | ||
| n | n | 108 | print(key) | ||
| 102 | if key in other.effects.keys(): | 109 | if key in other.effects.keys(): | ||
| n | n | 110 | if self.effects_intensity[key] == 1: | ||
| 103 | del new_effects[key] | 111 | del new_effects[key] | ||
| 104 | del new_intensity[key] | 112 | del new_intensity[key] | ||
| 105 | else: | 113 | else: | ||
| 106 | raise TypeError("Subtraction not possible due to missing effect/s...") | 114 | new_intensity[key] -= other.effects_intensity[key] | ||
| 115 | if new_intensity[key] <= 0: | ||||
| 116 | del new_effects[key] | ||||
| 117 | del new_intensity[key] | ||||
| 118 | |||||
| 107 | 119 | ||||
| 108 | new_potion = Potion(new_effects, self.duration) | 120 | new_potion = Potion(new_effects, self.duration) | ||
| 109 | new_potion.effects_intensity = new_intensity | 121 | new_potion.effects_intensity = new_intensity | ||
| 110 | 122 | ||||
| 111 | self.is_unusable = True | 123 | self.is_unusable = True | ||
| 112 | 124 | ||||
| 113 | return new_potion | 125 | return new_potion | ||
| 114 | 126 | ||||
| 115 | def __truediv__(self, number): | 127 | def __truediv__(self, number): | ||
| 116 | self.check_potion_obsolete() | 128 | self.check_potion_obsolete() | ||
| 117 | new_effects = self.effects.copy() | 129 | new_effects = self.effects.copy() | ||
| 118 | 130 | ||||
| n | 119 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] / new_effects) | n | 131 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] / number) |
| 120 | for intensity_effect in self.effects_intensity} | 132 | for intensity_effect in self.effects_intensity} | ||
| 121 | 133 | ||||
| 122 | new_potion = Potion(new_effects, self.duration) | 134 | new_potion = Potion(new_effects, self.duration) | ||
| 123 | new_potion.effects_intensity = new_intensity | 135 | new_potion.effects_intensity = new_intensity | ||
| n | n | 136 | new_potion.usable_effects = set(new_effects.keys()) | ||
| 124 | 137 | ||||
| 125 | new_potions = (new_potion, ) * number | 138 | new_potions = (new_potion, ) * number | ||
| 126 | 139 | ||||
| 127 | self.is_unusable = True | 140 | self.is_unusable = True | ||
| 128 | 141 | ||||
| 129 | return new_potions | 142 | return new_potions | ||
| 130 | 143 | ||||
| 131 | def __eq__(self, other): | 144 | def __eq__(self, other): | ||
| 132 | return self.effects == other.effects and self.effects_intensity == other.effects_intensity | 145 | return self.effects == other.effects and self.effects_intensity == other.effects_intensity | ||
| 133 | 146 | ||||
| 134 | def __gt__(self, other): | 147 | def __gt__(self, other): | ||
| 135 | return sum(self.effects_intensity.values()) > sum(other.effects_intensity.values()) | 148 | return sum(self.effects_intensity.values()) > sum(other.effects_intensity.values()) | ||
| 136 | 149 | ||||
| 137 | def __lt__(self, other): | 150 | def __lt__(self, other): | ||
| 138 | return sum(self.effects_intensity.values()) < sum(other.effects_intensity.values()) | 151 | return sum(self.effects_intensity.values()) < sum(other.effects_intensity.values()) | ||
| 139 | 152 | ||||
| n | n | 153 | def __getattr__(self, method_name): | ||
| 154 | return getattr(self, method_name)() | ||||
| 140 | class ГоспожатаПоХимия: | 155 | class ГоспожатаПоХимия: | ||
| 141 | def __init__(self): | 156 | def __init__(self): | ||
| 142 | self.applied_potions = {} | 157 | self.applied_potions = {} | ||
| 143 | self.given_target_copy = None | 158 | self.given_target_copy = None | ||
| 144 | self.current_target_object = None | 159 | self.current_target_object = None | ||
| 145 | 160 | ||||
| 146 | @staticmethod | 161 | @staticmethod | ||
| 147 | def sum_of_unicode_chars(string): | 162 | def sum_of_unicode_chars(string): | ||
| 148 | return sum(map(ord, string)) | 163 | return sum(map(ord, string)) | ||
| 149 | 164 | ||||
| 150 | def find_current_max_sum(self, names_of_effects): | 165 | def find_current_max_sum(self, names_of_effects): | ||
| 151 | max_sum = MIN_UNSIGNED_INT | 166 | max_sum = MIN_UNSIGNED_INT | ||
| 152 | max_sum_name = "" | 167 | max_sum_name = "" | ||
| 153 | for name in names_of_effects: | 168 | for name in names_of_effects: | ||
| 154 | if self.sum_of_unicode_chars(name) > max_sum: | 169 | if self.sum_of_unicode_chars(name) > max_sum: | ||
| 155 | max_sum = self.sum_of_unicode_chars(name) | 170 | max_sum = self.sum_of_unicode_chars(name) | ||
| 156 | max_sum_name = name | 171 | max_sum_name = name | ||
| 157 | 172 | ||||
| 158 | return max_sum_name | 173 | return max_sum_name | ||
| 159 | 174 | ||||
| 160 | def apply(self, target, potion): | 175 | def apply(self, target, potion): | ||
| 161 | names_of_effects = potion.usable_effects | 176 | names_of_effects = potion.usable_effects | ||
| n | 162 | self.applied_potions[potion] = potion.duration | n | 177 | self.applied_potions[tuple(names_of_effects)] = potion.duration |
| 163 | self.given_target_copy = copy.deepcopy(target) | 178 | self.given_target_copy = copy.deepcopy(target) | ||
| 164 | self.current_target_object = target | 179 | self.current_target_object = target | ||
| 165 | 180 | ||||
| 166 | if potion.is_unusable or len(potion.usable_effects) == 0: | 181 | if potion.is_unusable or len(potion.usable_effects) == 0: | ||
| 167 | raise TypeError("Potion is depleted.") | 182 | raise TypeError("Potion is depleted.") | ||
| 168 | else: | 183 | else: | ||
| 169 | potion.is_unusable = True | 184 | potion.is_unusable = True | ||
| 170 | 185 | ||||
| 171 | while len(potion.usable_effects) != 0: | 186 | while len(potion.usable_effects) != 0: | ||
| 172 | current_max_sum_name_effect = self.find_current_max_sum(names_of_effects) | 187 | current_max_sum_name_effect = self.find_current_max_sum(names_of_effects) | ||
| 173 | if current_max_sum_name_effect in potion.usable_effects: | 188 | if current_max_sum_name_effect in potion.usable_effects: | ||
| 174 | potion.current_max_sum_name_effect(target) | 189 | potion.current_max_sum_name_effect(target) | ||
| 175 | else: | 190 | else: | ||
| 176 | raise TypeError("Effect is depleted.") | 191 | raise TypeError("Effect is depleted.") | ||
| 177 | 192 | ||||
| 178 | 193 | ||||
| 179 | def tick(self): | 194 | def tick(self): | ||
| 180 | for potion in self.applied_potions: | 195 | for potion in self.applied_potions: | ||
| 181 | self.applied_potions[potion] -= 1 | 196 | self.applied_potions[potion] -= 1 | ||
| 182 | if self.applied_potions[potion] == 0: | 197 | if self.applied_potions[potion] == 0: | ||
| 183 | del self.applied_potions[potion] | 198 | del self.applied_potions[potion] | ||
| 184 | self.current_target_object = self.given_target_copy | 199 | self.current_target_object = self.given_target_copy | ||
| 185 | 200 | ||||
| 186 | 201 | ||||
| 187 | 202 | ||||
| 188 | 203 | ||||
| t | 189 | t | |||
| 190 | |||||
| 191 | |||||
| 192 | |||||
| 193 | |||||
| 194 | |||||
| 195 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | import copy | f | 1 | import copy |
| 2 | 2 | ||||
| 3 | MIN_UNSIGNED_INT = 0 | 3 | MIN_UNSIGNED_INT = 0 | ||
| 4 | 4 | ||||
| 5 | class Potion: | 5 | class Potion: | ||
| 6 | def __init__(self, effects, duration): | 6 | def __init__(self, effects, duration): | ||
| 7 | self.effects = effects | 7 | self.effects = effects | ||
| 8 | self.duration = duration | 8 | self.duration = duration | ||
| 9 | 9 | ||||
| 10 | self.effects_intensity = {} | 10 | self.effects_intensity = {} | ||
| 11 | self.create_intensity() | 11 | self.create_intensity() | ||
| 12 | 12 | ||||
| 13 | self.set_effects_as_methods() | 13 | self.set_effects_as_methods() | ||
| 14 | 14 | ||||
| 15 | self.is_unusable = False | 15 | self.is_unusable = False | ||
| 16 | self.usable_effects = set(self.effects.keys()) | 16 | self.usable_effects = set(self.effects.keys()) | ||
| 17 | 17 | ||||
| 18 | @staticmethod | 18 | @staticmethod | ||
| 19 | def rounded(number): | 19 | def rounded(number): | ||
| 20 | rounded_num = round(number) | 20 | rounded_num = round(number) | ||
| 21 | return rounded_num + 1 if number - rounded_num > 0.5 else rounded_num | 21 | return rounded_num + 1 if number - rounded_num > 0.5 else rounded_num | ||
| 22 | 22 | ||||
| 23 | def set_effects_as_methods(self): | 23 | def set_effects_as_methods(self): | ||
| 24 | for effect in self.effects: | 24 | for effect in self.effects: | ||
| 25 | setattr(self, effect, self.log_method_callings(effect, self.effects[effect])) | 25 | setattr(self, effect, self.log_method_callings(effect, self.effects[effect])) | ||
| 26 | def check_potion_obsolete(self): | 26 | def check_potion_obsolete(self): | ||
| 27 | if self.is_unusable: | 27 | if self.is_unusable: | ||
| 28 | raise TypeError("Potion is now part of something bigger than itself.") | 28 | raise TypeError("Potion is now part of something bigger than itself.") | ||
| 29 | 29 | ||||
| 30 | def log_method_callings(self, effect, method): | 30 | def log_method_callings(self, effect, method): | ||
| 31 | def wrapper(*args, **kwargs): | 31 | def wrapper(*args, **kwargs): | ||
| 32 | if effect not in self.usable_effects: | 32 | if effect not in self.usable_effects: | ||
| 33 | raise TypeError("Effect is depleted.") | 33 | raise TypeError("Effect is depleted.") | ||
| 34 | 34 | ||||
| 35 | self.usable_effects.remove(effect) | 35 | self.usable_effects.remove(effect) | ||
| 36 | 36 | ||||
| 37 | for times in range(0, self.effects_intensity[effect]): | 37 | for times in range(0, self.effects_intensity[effect]): | ||
| 38 | if self.effects_intensity[effect] == 1: | 38 | if self.effects_intensity[effect] == 1: | ||
| 39 | return method | 39 | return method | ||
| 40 | else: | 40 | else: | ||
| 41 | method() | 41 | method() | ||
| 42 | 42 | ||||
| 43 | return method | 43 | return method | ||
| 44 | return wrapper | 44 | return wrapper | ||
| 45 | 45 | ||||
| 46 | def create_intensity(self): | 46 | def create_intensity(self): | ||
| 47 | self.effects_intensity = {effect:1 for effect in self.effects} | 47 | self.effects_intensity = {effect:1 for effect in self.effects} | ||
| 48 | 48 | ||||
| 49 | def __add__(self, other): | 49 | def __add__(self, other): | ||
| 50 | self.check_potion_obsolete() | 50 | self.check_potion_obsolete() | ||
| 51 | new_effects = self.effects.copy() | 51 | new_effects = self.effects.copy() | ||
| 52 | new_intensity = self.effects_intensity.copy() | 52 | new_intensity = self.effects_intensity.copy() | ||
| 53 | new_duration = self.duration | 53 | new_duration = self.duration | ||
| 54 | #effects | 54 | #effects | ||
| 55 | for effect in self.effects: | 55 | for effect in self.effects: | ||
| 56 | #if the same | 56 | #if the same | ||
| 57 | if effect in other.effects: | 57 | if effect in other.effects: | ||
| 58 | new_intensity[effect] += 1 | 58 | new_intensity[effect] += 1 | ||
| 59 | #if not the same | 59 | #if not the same | ||
| 60 | else: | 60 | else: | ||
| 61 | new_effects[effect] = other.effects[effect] | 61 | new_effects[effect] = other.effects[effect] | ||
| 62 | new_intensity[effect] = 1 | 62 | new_intensity[effect] = 1 | ||
| 63 | #duration | 63 | #duration | ||
| 64 | if self.duration < other.duration: | 64 | if self.duration < other.duration: | ||
| 65 | new_duration = other.duration | 65 | new_duration = other.duration | ||
| 66 | 66 | ||||
| 67 | new_potion = Potion(new_effects, new_duration) | 67 | new_potion = Potion(new_effects, new_duration) | ||
| 68 | new_potion.effects_intensity = new_intensity | 68 | new_potion.effects_intensity = new_intensity | ||
| 69 | new_potion.usable_effects = self.usable_effects | 69 | new_potion.usable_effects = self.usable_effects | ||
| 70 | 70 | ||||
| 71 | self.is_unusable = True | 71 | self.is_unusable = True | ||
| 72 | 72 | ||||
| 73 | return new_potion | 73 | return new_potion | ||
| 74 | 74 | ||||
| 75 | def __mul__(self, number): | 75 | def __mul__(self, number): | ||
| 76 | self.check_potion_obsolete() | 76 | self.check_potion_obsolete() | ||
| 77 | if number>0 and isinstance(number, int): | 77 | if number>0 and isinstance(number, int): | ||
| 78 | new_intensity = {intensity_effect: self.effects_intensity[intensity_effect] * number | 78 | new_intensity = {intensity_effect: self.effects_intensity[intensity_effect] * number | ||
| 79 | for intensity_effect in self.effects_intensity} | 79 | for intensity_effect in self.effects_intensity} | ||
| 80 | 80 | ||||
| 81 | new_potion = Potion(self.effects, self.duration) | 81 | new_potion = Potion(self.effects, self.duration) | ||
| 82 | new_potion.effects_intensity = new_intensity | 82 | new_potion.effects_intensity = new_intensity | ||
| 83 | 83 | ||||
| 84 | return new_potion | 84 | return new_potion | ||
| 85 | 85 | ||||
| 86 | elif 0 <= number <= 1 and isinstance(number, float): | 86 | elif 0 <= number <= 1 and isinstance(number, float): | ||
| 87 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] * number) | 87 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] * number) | ||
| 88 | for intensity_effect in self.effects_intensity} | 88 | for intensity_effect in self.effects_intensity} | ||
| 89 | 89 | ||||
| 90 | new_potion = Potion(self.effects, self.duration) | 90 | new_potion = Potion(self.effects, self.duration) | ||
| 91 | new_potion.effects_intensity = new_intensity | 91 | new_potion.effects_intensity = new_intensity | ||
| 92 | 92 | ||||
| 93 | self.is_unusable = True | 93 | self.is_unusable = True | ||
| 94 | 94 | ||||
| 95 | return new_potion | 95 | return new_potion | ||
| 96 | 96 | ||||
| 97 | def __sub__(self, other): | 97 | def __sub__(self, other): | ||
| 98 | self.check_potion_obsolete() | 98 | self.check_potion_obsolete() | ||
| 99 | new_effects = self.effects.copy() | 99 | new_effects = self.effects.copy() | ||
| 100 | new_intensity = self.effects_intensity.copy() | 100 | new_intensity = self.effects_intensity.copy() | ||
| 101 | for key in self.effects.keys(): | 101 | for key in self.effects.keys(): | ||
| 102 | if key in other.effects.keys(): | 102 | if key in other.effects.keys(): | ||
| 103 | del new_effects[key] | 103 | del new_effects[key] | ||
| 104 | del new_intensity[key] | 104 | del new_intensity[key] | ||
| 105 | else: | 105 | else: | ||
| 106 | raise TypeError("Subtraction not possible due to missing effect/s...") | 106 | raise TypeError("Subtraction not possible due to missing effect/s...") | ||
| 107 | 107 | ||||
| 108 | new_potion = Potion(new_effects, self.duration) | 108 | new_potion = Potion(new_effects, self.duration) | ||
| 109 | new_potion.effects_intensity = new_intensity | 109 | new_potion.effects_intensity = new_intensity | ||
| 110 | 110 | ||||
| 111 | self.is_unusable = True | 111 | self.is_unusable = True | ||
| 112 | 112 | ||||
| 113 | return new_potion | 113 | return new_potion | ||
| 114 | 114 | ||||
| 115 | def __truediv__(self, number): | 115 | def __truediv__(self, number): | ||
| 116 | self.check_potion_obsolete() | 116 | self.check_potion_obsolete() | ||
| 117 | new_effects = self.effects.copy() | 117 | new_effects = self.effects.copy() | ||
| 118 | 118 | ||||
| 119 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] / new_effects) | 119 | new_intensity = {intensity_effect: self.rounded(self.effects_intensity[intensity_effect] / new_effects) | ||
| 120 | for intensity_effect in self.effects_intensity} | 120 | for intensity_effect in self.effects_intensity} | ||
| 121 | 121 | ||||
| 122 | new_potion = Potion(new_effects, self.duration) | 122 | new_potion = Potion(new_effects, self.duration) | ||
| 123 | new_potion.effects_intensity = new_intensity | 123 | new_potion.effects_intensity = new_intensity | ||
| 124 | 124 | ||||
| 125 | new_potions = (new_potion, ) * number | 125 | new_potions = (new_potion, ) * number | ||
| 126 | 126 | ||||
| 127 | self.is_unusable = True | 127 | self.is_unusable = True | ||
| 128 | 128 | ||||
| 129 | return new_potions | 129 | return new_potions | ||
| 130 | 130 | ||||
| 131 | def __eq__(self, other): | 131 | def __eq__(self, other): | ||
| 132 | return self.effects == other.effects and self.effects_intensity == other.effects_intensity | 132 | return self.effects == other.effects and self.effects_intensity == other.effects_intensity | ||
| 133 | 133 | ||||
| 134 | def __gt__(self, other): | 134 | def __gt__(self, other): | ||
| 135 | return sum(self.effects_intensity.values()) > sum(other.effects_intensity.values()) | 135 | return sum(self.effects_intensity.values()) > sum(other.effects_intensity.values()) | ||
| 136 | 136 | ||||
| 137 | def __lt__(self, other): | 137 | def __lt__(self, other): | ||
| 138 | return sum(self.effects_intensity.values()) < sum(other.effects_intensity.values()) | 138 | return sum(self.effects_intensity.values()) < sum(other.effects_intensity.values()) | ||
| 139 | 139 | ||||
| t | 140 | def __call__(self, *args, **kwargs): | t | ||
| 141 | #for calling effects and using intensity logic | ||||
| 142 | pass | ||||
| 143 | |||||
| 144 | |||||
| 145 | class ГоспожатаПоХимия: | 140 | class ГоспожатаПоХимия: | ||
| 146 | def __init__(self): | 141 | def __init__(self): | ||
| 147 | self.applied_potions = {} | 142 | self.applied_potions = {} | ||
| 148 | self.given_target_copy = None | 143 | self.given_target_copy = None | ||
| 149 | self.current_target_object = None | 144 | self.current_target_object = None | ||
| 150 | 145 | ||||
| 151 | @staticmethod | 146 | @staticmethod | ||
| 152 | def sum_of_unicode_chars(string): | 147 | def sum_of_unicode_chars(string): | ||
| 153 | return sum(map(ord, string)) | 148 | return sum(map(ord, string)) | ||
| 154 | 149 | ||||
| 155 | def find_current_max_sum(self, names_of_effects): | 150 | def find_current_max_sum(self, names_of_effects): | ||
| 156 | max_sum = MIN_UNSIGNED_INT | 151 | max_sum = MIN_UNSIGNED_INT | ||
| 157 | max_sum_name = "" | 152 | max_sum_name = "" | ||
| 158 | for name in names_of_effects: | 153 | for name in names_of_effects: | ||
| 159 | if self.sum_of_unicode_chars(name) > max_sum: | 154 | if self.sum_of_unicode_chars(name) > max_sum: | ||
| 160 | max_sum = self.sum_of_unicode_chars(name) | 155 | max_sum = self.sum_of_unicode_chars(name) | ||
| 161 | max_sum_name = name | 156 | max_sum_name = name | ||
| 162 | 157 | ||||
| 163 | return max_sum_name | 158 | return max_sum_name | ||
| 164 | 159 | ||||
| 165 | def apply(self, target, potion): | 160 | def apply(self, target, potion): | ||
| 166 | names_of_effects = potion.usable_effects | 161 | names_of_effects = potion.usable_effects | ||
| 167 | self.applied_potions[potion] = potion.duration | 162 | self.applied_potions[potion] = potion.duration | ||
| 168 | self.given_target_copy = copy.deepcopy(target) | 163 | self.given_target_copy = copy.deepcopy(target) | ||
| 169 | self.current_target_object = target | 164 | self.current_target_object = target | ||
| 170 | 165 | ||||
| 171 | if potion.is_unusable or len(potion.usable_effects) == 0: | 166 | if potion.is_unusable or len(potion.usable_effects) == 0: | ||
| 172 | raise TypeError("Potion is depleted.") | 167 | raise TypeError("Potion is depleted.") | ||
| 173 | else: | 168 | else: | ||
| 174 | potion.is_unusable = True | 169 | potion.is_unusable = True | ||
| 175 | 170 | ||||
| 176 | while len(potion.usable_effects) != 0: | 171 | while len(potion.usable_effects) != 0: | ||
| 177 | current_max_sum_name_effect = self.find_current_max_sum(names_of_effects) | 172 | current_max_sum_name_effect = self.find_current_max_sum(names_of_effects) | ||
| 178 | if current_max_sum_name_effect in potion.usable_effects: | 173 | if current_max_sum_name_effect in potion.usable_effects: | ||
| 179 | potion.current_max_sum_name_effect(target) | 174 | potion.current_max_sum_name_effect(target) | ||
| 180 | else: | 175 | else: | ||
| 181 | raise TypeError("Effect is depleted.") | 176 | raise TypeError("Effect is depleted.") | ||
| 182 | 177 | ||||
| 183 | 178 | ||||
| 184 | def tick(self): | 179 | def tick(self): | ||
| 185 | for potion in self.applied_potions: | 180 | for potion in self.applied_potions: | ||
| 186 | self.applied_potions[potion] -= 1 | 181 | self.applied_potions[potion] -= 1 | ||
| 187 | if self.applied_potions[potion] == 0: | 182 | if self.applied_potions[potion] == 0: | ||
| 188 | del self.applied_potions[potion] | 183 | del self.applied_potions[potion] | ||
| 189 | self.current_target_object = self.given_target_copy | 184 | self.current_target_object = self.given_target_copy | ||
| 190 | 185 | ||||
| 191 | 186 | ||||
| 192 | 187 | ||||
| 193 | 188 | ||||
| 194 | 189 | ||||
| 195 | 190 | ||||
| 196 | 191 | ||||
| 197 | 192 | ||||
| 198 | 193 | ||||
| 199 | 194 | ||||
| 200 | 195 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
08.12.2023 14:01
08.12.2023 14:02
08.12.2023 14:02
08.12.2023 14:03
08.12.2023 14:04
08.12.2023 14:05
08.12.2023 14:06