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

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

0 точки общо

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

  1class Potion:
  2    def __init__(effects, duration):
  3        self.__effects__ = {}
  4        self.__duration__ = duration
  5        self.__is_used__ = False
  6
  7        for key, effect in effects.items():
  8            self.__effects__[key] = (1, effect)
  9
 10            setattr(self, key, lambda effect_name=key: self.apply_effect(effect_name))
 11
 12    def apply_effect(self, effect_key):
 13        if self.__is_used__:
 14            Potion.__raise_exception__()
 15
 16        intensity, lambda_effect = self.__effects__.get(effect_key, (0, None))
 17
 18        for _ in range(intensity):
 19            lambda_effect()
 20
 21    @staticmethod
 22    def __raise_exception__():
 23        raise TypeError("Potion is now part of something bigger than itself.")
 24
 25    def set_used(self):
 26        self.__is_used__ = True
 27
 28        for key in self.__effects__.keys():
 29            self.__effects__[key] = Potion.__raise_exception__
 30
 31    @staticmethod
 32    def check_and_set_used(*args):
 33        for obj in args:
 34            if obj.__is_used__:
 35                Potion.__raise_exception()
 36
 37            obj.set_used()
 38
 39    @staticmethod
 40    def __combine_effects__(effects1, effects2):
 41        combined_effects = {}
 42
 43        for key, (first_intensity, first_effect) in effects1.items():
 44            second_intensity = effects2[key][0] if key in effects2 else 0
 45
 46            combined_effects[key] = (first_intensity + second_intensity, first_effect)
 47
 48        for key, (second_intensity, second_effect) in effects2.items():
 49            if key not in effects1:
 50                combined_effects[key] = (second_intensity, second_effect)
 51
 52        return combined_effects
 53
 54    def __add__(self, other):
 55        Potion.check_and_set_used(self, other)
 56
 57        return Potion(Potion.__combine_effects__(self.__effects__, other.__effects__), max(self.__duration__, other.__duration__))
 58
 59    # тъпо, защото 0.5 трябва да се закръгля до 1, а не до 0.
 60    @staticmethod
 61    def __custom_round__(x):
 62        if x - int(x) >= 0.5:
 63            return int(x) + 1
 64        else:
 65            return int(x)
 66
 67    def __mul__(self, multiplier):
 68        Potion.check_and_set_used(self)
 69
 70        for key, (intensity, effect) in self.__effects__.items():
 71            self.__effects__[key] = (Potion.__custom_round__(intensity * multiplier), effect)
 72
 73    @staticmethod
 74    def __remove_effects__(effects1, effects2):
 75        removed_effects = {}
 76
 77        if set(dict2.keys()) - set(dict1.keys()):
 78            raise TypeError("Мамка му, аз нямам такъв ефект.")
 79
 80        for key, (first_intensity, effect) in effects1.items():
 81            second_intensity = effects2.get(key, (None, None))[0]
 82
 83            if second_intensity is None:
 84                removed_effects[key] = (first_intensity, effect)
 85            else if first_intensity > second_intensity:
 86                removed_effects[key] = (first_intensity - second_intensity, effect)
 87
 88        return removed_effects
 89
 90    def __sub__(self, other):
 91        Potion.check_and_set_used(self, other)
 92
 93        return Potion(Potion.__remove_effects__(self.__effects__, other.__effects__), self.__duration__)
 94
 95    def __truediv__(self, divider):
 96        Potion.check_and_set_used(self)
 97
 98        result_potion = self * (1 / divider)
 99
100        return (result_potion, ) * divider
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116# effects = {'grow': lambda target: setattr(target, 'size', target.size*2)} 

expected ':' (solution.py, line 87)
File "/tmp/test_runner.py", line 75, in main
loaded_test = importlib.import_module('test', test_module)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/tmp/test.py", line 4, in <module>
from solution import *

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

f1f1
22
3class Potion:3class Potion:
4    def __init__(effects, duration):4    def __init__(effects, duration):
5        self.__effects__ = {}5        self.__effects__ = {}
6        self.__duration__ = duration6        self.__duration__ = duration
7        self.__is_used__ = False7        self.__is_used__ = False
88
9        for key, effect in effects.items():9        for key, effect in effects.items():
10            self.__effects__[key] = (1, effect)10            self.__effects__[key] = (1, effect)
1111
12            setattr(self, key, lambda effect_name=key: self.apply_effect(effect_name))12            setattr(self, key, lambda effect_name=key: self.apply_effect(effect_name))
1313
14    def apply_effect(self, effect_key):14    def apply_effect(self, effect_key):
15        if self.__is_used__:15        if self.__is_used__:
16            Potion.__raise_exception__()16            Potion.__raise_exception__()
1717
18        intensity, lambda_effect = self.__effects__.get(effect_key, (0, None))18        intensity, lambda_effect = self.__effects__.get(effect_key, (0, None))
1919
20        for _ in range(intensity):20        for _ in range(intensity):
21            lambda_effect()21            lambda_effect()
2222
23    @staticmethod23    @staticmethod
24    def __raise_exception__():24    def __raise_exception__():
25        raise TypeError("Potion is now part of something bigger than itself.")25        raise TypeError("Potion is now part of something bigger than itself.")
2626
27    def set_used(self):27    def set_used(self):
28        self.__is_used__ = True28        self.__is_used__ = True
2929
30        for key in self.__effects__.keys():30        for key in self.__effects__.keys():
31            self.__effects__[key] = Potion.__raise_exception__31            self.__effects__[key] = Potion.__raise_exception__
3232
33    @staticmethod33    @staticmethod
34    def check_and_set_used(*args):34    def check_and_set_used(*args):
35        for obj in args:35        for obj in args:
36            if obj.__is_used__:36            if obj.__is_used__:
37                Potion.__raise_exception()37                Potion.__raise_exception()
3838
39            obj.set_used()39            obj.set_used()
4040
41    @staticmethod41    @staticmethod
42    def __combine_effects__(effects1, effects2):42    def __combine_effects__(effects1, effects2):
43        combined_effects = {}43        combined_effects = {}
4444
45        for key, (first_intensity, first_effect) in effects1.items():45        for key, (first_intensity, first_effect) in effects1.items():
46            second_intensity = effects2[key][0] if key in effects2 else 046            second_intensity = effects2[key][0] if key in effects2 else 0
4747
48            combined_effects[key] = (first_intensity + second_intensity, first_effect)48            combined_effects[key] = (first_intensity + second_intensity, first_effect)
4949
50        for key, (second_intensity, second_effect) in effects2.items():50        for key, (second_intensity, second_effect) in effects2.items():
51            if key not in effects1:51            if key not in effects1:
52                combined_effects[key] = (second_intensity, second_effect)52                combined_effects[key] = (second_intensity, second_effect)
5353
54        return combined_effects54        return combined_effects
5555
56    def __add__(self, other):56    def __add__(self, other):
57        Potion.check_and_set_used(self, other)57        Potion.check_and_set_used(self, other)
5858
59        return Potion(Potion.__combine_effects__(self.__effects__, other.__effects__), max(self.__duration__, other.__duration__))59        return Potion(Potion.__combine_effects__(self.__effects__, other.__effects__), max(self.__duration__, other.__duration__))
6060
t61    # да го духате, защото 0.5 трябва да се закръгля до 1, а не до 0.t61    # тъпо, защото 0.5 трябва да се закръгля до 1, а не до 0.
62    @staticmethod62    @staticmethod
63    def __custom_round__(x):63    def __custom_round__(x):
64        if x - int(x) >= 0.5:64        if x - int(x) >= 0.5:
65            return int(x) + 165            return int(x) + 1
66        else:66        else:
67            return int(x)67            return int(x)
6868
69    def __mul__(self, multiplier):69    def __mul__(self, multiplier):
70        Potion.check_and_set_used(self)70        Potion.check_and_set_used(self)
7171
72        for key, (intensity, effect) in self.__effects__.items():72        for key, (intensity, effect) in self.__effects__.items():
73            self.__effects__[key] = (Potion.__custom_round__(intensity * multiplier), effect)73            self.__effects__[key] = (Potion.__custom_round__(intensity * multiplier), effect)
7474
75    @staticmethod75    @staticmethod
76    def __remove_effects__(effects1, effects2):76    def __remove_effects__(effects1, effects2):
77        removed_effects = {}77        removed_effects = {}
7878
79        if set(dict2.keys()) - set(dict1.keys()):79        if set(dict2.keys()) - set(dict1.keys()):
80            raise TypeError("Мамка му, аз нямам такъв ефект.")80            raise TypeError("Мамка му, аз нямам такъв ефект.")
8181
82        for key, (first_intensity, effect) in effects1.items():82        for key, (first_intensity, effect) in effects1.items():
83            second_intensity = effects2.get(key, (None, None))[0]83            second_intensity = effects2.get(key, (None, None))[0]
8484
85            if second_intensity is None:85            if second_intensity is None:
86                removed_effects[key] = (first_intensity, effect)86                removed_effects[key] = (first_intensity, effect)
87            else if first_intensity > second_intensity:87            else if first_intensity > second_intensity:
88                removed_effects[key] = (first_intensity - second_intensity, effect)88                removed_effects[key] = (first_intensity - second_intensity, effect)
8989
90        return removed_effects90        return removed_effects
9191
92    def __sub__(self, other):92    def __sub__(self, other):
93        Potion.check_and_set_used(self, other)93        Potion.check_and_set_used(self, other)
9494
95        return Potion(Potion.__remove_effects__(self.__effects__, other.__effects__), self.__duration__)95        return Potion(Potion.__remove_effects__(self.__effects__, other.__effects__), self.__duration__)
9696
97    def __truediv__(self, divider):97    def __truediv__(self, divider):
98        Potion.check_and_set_used(self)98        Potion.check_and_set_used(self)
9999
100        result_potion = self * (1 / divider)100        result_potion = self * (1 / divider)
101101
102        return (result_potion, ) * divider102        return (result_potion, ) * divider
103103
104104
105105
106106
107107
108108
109109
110110
111111
112112
113113
114114
115115
116116
117117
118# effects = {'grow': lambda target: setattr(target, 'size', target.size*2)} 118# effects = {'grow': lambda target: setattr(target, 'size', target.size*2)} 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op