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

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

7 точки общо

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

  1def get_molecule_mass(effect_name):
  2    molecule_mass = 0
  3    for letter in effect_name:
  4        print(letter)
  5        molecule_mass += ord(letter)
  6        
  7    return molecule_mass
  8
  9class Effect:
 10    
 11    def __init__(self, name, effect):
 12        self.name = name
 13        self.effect = effect
 14        self.part_of_something_bigger = False
 15        self.called = False
 16        self.intensity = 1
 17        
 18    def increment_intensity(self):
 19        self.intensity += 1  
 20    
 21    def __call__(self, target):
 22        if self.called:
 23            raise TypeError('Effect is depleted.')
 24        
 25        if self.part_of_something_bigger:
 26            raise TypeError('Potion is now part of something bigger than itself.')
 27        
 28        for _ in range (self.intensity):
 29            self.effect(target)
 30        self.called = True
 31
 32
 33class Potion:
 34    
 35    def __init__(self, effects, duration):
 36        self.effect = {}
 37        self.object_effects = {}
 38        self.duration = duration
 39        self.called = False
 40        self.part_of_something_bigger = False
 41        for effect_name in effects.keys():
 42            if effect_name in self.effect.keys():
 43                self.object_effects[effect_name].increment_intensity()
 44            else:
 45                current_effect = Effect(effect_name, effects[effect_name])
 46                self.effect[effect_name] = effects[effect_name]
 47                self.object_effects[effect_name] = current_effect
 48                setattr(self, effect_name, current_effect)
 49    
 50    def __add__(self, other):
 51        if self.called is True or other.called is True:
 52            raise TypeError('Potion is depleted.')
 53        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:
 54            raise TypeError('Potion is now part of something bigger than itself.')
 55        self.part_of_something_bigger = True
 56        other.part_of_something_bigger = True
 57        new_potion_effects = self.effect | other.effect
 58        new_potion_duration = max(self.duration, other.duration)
 59        new_potion =  Potion(new_potion_effects, new_potion_duration)
 60        
 61        for effect_name in new_potion.object_effects:
 62            new_potion.object_effects[effect_name].intensity = 0
 63            if effect_name in self.object_effects and not self.object_effects[effect_name].called:
 64                new_potion.object_effects[effect_name].intensity += self.object_effects[effect_name].intensity
 65                self.object_effects[effect_name].part_of_something_bigger = True
 66            if effect_name in other.object_effects and not other.object_effects[effect_name].called:
 67                new_potion.object_effects[effect_name].intensity += other.object_effects[effect_name].intensity
 68                other.object_effects[effect_name].part_of_something_bigger = True
 69        
 70        for effect_name in self.object_effects:
 71            if (effect_name in other.object_effects and
 72                self.object_effects[effect_name].called and
 73                other.object_effects[effect_name].called):
 74                new_potion.object_effects.pop(effect_name)
 75                new_potion.effect.pop(effect_name)
 76                delattr(new_potion, effect_name)
 77        
 78        return new_potion
 79    
 80    def __mul__(self, other):
 81        if self.called:
 82            raise TypeError('Potion is depleted.')
 83        if self.part_of_something_bigger:
 84            raise TypeError('Potion is now part of something bigger than itself.')
 85        self.part_of_something_bigger = True
 86        new_potion = Potion(self.effect, self.duration)
 87        
 88        for effect_name in self.object_effects:
 89            if self.object_effects[effect_name].called:
 90                new_potion.object_effects.pop(effect_name)
 91                new_potion.effect.pop(effect_name)
 92                delattr(new_potion, effect_name)
 93                continue
 94            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity
 95            self.object_effects[effect_name].part_of_something_bigger = True
 96        for effect_name in new_potion.object_effects:
 97            new_potion.object_effects[effect_name].intensity = int(round(new_potion.object_effects[effect_name].intensity * other, 0))
 98        return new_potion
 99    
100    def __sub__(self, other):
101        if self.called is True or other.called is True:
102            raise TypeError('Potion is depleted.')
103        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:
104            raise TypeError('Potion is now part of something bigger than itself.')
105        self.part_of_something_bigger = True
106        other.part_of_something_bigger = True
107        new_potion = Potion(self.effect, self.duration)
108        
109        for effect_name in self.object_effects:
110            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity
111            self.object_effects[effect_name].part_of_something_bigger = True
112            if effect_name in other.object_effects:
113                other.object_effects[effect_name].part_of_something_bigger = True
114
115        for effect_name in other.object_effects:
116            if effect_name not in  new_potion.object_effects.keys():
117                raise TypeError('cannot remove non-existent effects')
118            if other.object_effects[effect_name].called:
119                continue
120            if new_potion.object_effects[effect_name].intensity > other.object_effects[effect_name].intensity:
121                new_potion.object_effects[effect_name].intensity -= other.object_effects[effect_name].intensity
122            else:
123                new_potion.object_effects.pop(effect_name)
124                new_potion.effect.pop(effect_name)
125                delattr(new_potion, effect_name)
126                
127        for effect_name in self.object_effects:
128            if self.object_effects[effect_name].called:
129                new_potion.object_effects.pop(effect_name)
130                new_potion.effect.pop(effect_name)
131                delattr(new_potion, effect_name)
132        return new_potion
133    
134    def __truediv__(self, other): 
135        potions = []
136        for _ in range(other):
137            potions.append(self * (1 / other))
138            self.part_of_something_bigger = False
139            for effect_name in self.object_effects:
140                self.object_effects[effect_name].part_of_something_bigger = False
141        
142        self.part_of_something_bigger = True
143        for effect_name in self.object_effects:
144            self.object_effects[effect_name].part_of_something_bigger = True
145        return potions
146    
147    def __eq__(self, other):
148        if self.called is True or other.called is True:
149            raise TypeError('Potion is depleted.')
150        if self.effect != other.effect:
151            return False
152        for effect_name in self.object_effects:
153            if (effect_name not in other.object_effects or 
154                    other.object_effects[effect_name].intensity != self.object_effects[effect_name].intensity):
155                return False
156        
157        return True
158    
159    def __lt__(self, other):
160        if self.called is True or other.called is True:
161            raise TypeError('Potion is depleted.')
162        self_intensity = 0
163        for effect in self.object_effects.values():
164            self_intensity += effect.intensity
165        other_intensity = 0
166        for effect in other.object_effects.values():
167            other_intensity += effect.intensity
168            
169        return self_intensity < other_intensity
170    
171    def __gt__(self, other):
172        return other < self
173    
174    
175class ГоспожатаПоХимия:
176    
177    def apply(self, target, potion):
178        if potion.called is True:
179            raise TypeError('Potion is depleted.')
180        
181        sorted_effects = list(sorted(potion.object_effects, key=lambda item: get_molecule_mass(item), reverse=True))
182        for effect in sorted_effects:
183            if potion.object_effects[effect].called is not True:
184                potion.object_effects[effect](target)
185        potion.called = True
186        
187    def tick(self):
188        pass
189        

........F..F....F
Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
F
Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
i
n
t
_
a
t
t
r
_
f
u
n
F
Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
i
n
t
_
a
t
t
r
_
f
u
n
F
Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
f
l
o
a
t
_
a
t
t
r
_
f
u
n
l
i
s
t
_
a
t
t
r
_
f
u
n
d
i
c
t
_
a
t
t
r
_
f
u
n

======================================================================
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

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

======================================================================
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 429, in test_ticking_immutable
self.assertEqual(self._target.int_attr, 5)
AssertionError: 500 != 5

Stdout:
i
n
t
_
a
t
t
r
_
f
u
n

======================================================================
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

Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
i
n
t
_
a
t
t
r
_
f
u
n

======================================================================
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

Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
i
n
t
_
a
t
t
r
_
f
u
n

======================================================================
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

Stdout:
i
n
t
_
a
t
t
r
_
f
u
n
f
l
o
a
t
_
a
t
t
r
_
f
u
n
l
i
s
t
_
a
t
t
r
_
f
u
n
d
i
c
t
_
a
t
t
r
_
f
u
n

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

FAILED (failures=6)

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

f1def get_molecule_mass(effect_name):f1def get_molecule_mass(effect_name):
2    molecule_mass = 02    molecule_mass = 0
3    for letter in effect_name:3    for letter in effect_name:
4        print(letter)4        print(letter)
5        molecule_mass += ord(letter)5        molecule_mass += ord(letter)
6        6        
7    return molecule_mass7    return molecule_mass
88
9class Effect:9class Effect:
10    10    
11    def __init__(self, name, effect):11    def __init__(self, name, effect):
12        self.name = name12        self.name = name
13        self.effect = effect13        self.effect = effect
14        self.part_of_something_bigger = False14        self.part_of_something_bigger = False
15        self.called = False15        self.called = False
16        self.intensity = 116        self.intensity = 1
17        17        
18    def increment_intensity(self):18    def increment_intensity(self):
19        self.intensity += 1  19        self.intensity += 1  
20    20    
21    def __call__(self, target):21    def __call__(self, target):
22        if self.called:22        if self.called:
23            raise TypeError('Effect is depleted.')23            raise TypeError('Effect is depleted.')
24        24        
25        if self.part_of_something_bigger:25        if self.part_of_something_bigger:
26            raise TypeError('Potion is now part of something bigger than itself.')26            raise TypeError('Potion is now part of something bigger than itself.')
27        27        
28        for _ in range (self.intensity):28        for _ in range (self.intensity):
29            self.effect(target)29            self.effect(target)
30        self.called = True30        self.called = True
3131
3232
33class Potion:33class Potion:
34    34    
35    def __init__(self, effects, duration):35    def __init__(self, effects, duration):
36        self.effect = {}36        self.effect = {}
37        self.object_effects = {}37        self.object_effects = {}
38        self.duration = duration38        self.duration = duration
39        self.called = False39        self.called = False
40        self.part_of_something_bigger = False40        self.part_of_something_bigger = False
41        for effect_name in effects.keys():41        for effect_name in effects.keys():
42            if effect_name in self.effect.keys():42            if effect_name in self.effect.keys():
43                self.object_effects[effect_name].increment_intensity()43                self.object_effects[effect_name].increment_intensity()
44            else:44            else:
45                current_effect = Effect(effect_name, effects[effect_name])45                current_effect = Effect(effect_name, effects[effect_name])
46                self.effect[effect_name] = effects[effect_name]46                self.effect[effect_name] = effects[effect_name]
47                self.object_effects[effect_name] = current_effect47                self.object_effects[effect_name] = current_effect
48                setattr(self, effect_name, current_effect)48                setattr(self, effect_name, current_effect)
49    49    
50    def __add__(self, other):50    def __add__(self, other):
51        if self.called is True or other.called is True:51        if self.called is True or other.called is True:
52            raise TypeError('Potion is depleted.')52            raise TypeError('Potion is depleted.')
53        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:53        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:
54            raise TypeError('Potion is now part of something bigger than itself.')54            raise TypeError('Potion is now part of something bigger than itself.')
55        self.part_of_something_bigger = True55        self.part_of_something_bigger = True
56        other.part_of_something_bigger = True56        other.part_of_something_bigger = True
57        new_potion_effects = self.effect | other.effect57        new_potion_effects = self.effect | other.effect
58        new_potion_duration = max(self.duration, other.duration)58        new_potion_duration = max(self.duration, other.duration)
59        new_potion =  Potion(new_potion_effects, new_potion_duration)59        new_potion =  Potion(new_potion_effects, new_potion_duration)
60        60        
61        for effect_name in new_potion.object_effects:61        for effect_name in new_potion.object_effects:
n62            if effect_name in self.object_effects:n62            new_potion.object_effects[effect_name].intensity = 0
63            if effect_name in self.object_effects and not self.object_effects[effect_name].called:
63                new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity64                new_potion.object_effects[effect_name].intensity += self.object_effects[effect_name].intensity
64                self.object_effects[effect_name].part_of_something_bigger = True65                self.object_effects[effect_name].part_of_something_bigger = True
n65            else:n66            if effect_name in other.object_effects and not other.object_effects[effect_name].called:
66                new_potion.object_effects[effect_name].intensity = other.object_effects[effect_name].intensity67                new_potion.object_effects[effect_name].intensity += other.object_effects[effect_name].intensity
67                other.object_effects[effect_name].part_of_something_bigger = True68                other.object_effects[effect_name].part_of_something_bigger = True
68        69        
n69        for effect_name in self.effect:n70        for effect_name in self.object_effects:
70            if effect_name in other.effect:71            if (effect_name in other.object_effects and
71                new_potion.object_effects[effect_name].intensity += other.object_effects[effect_name].intensity72                self.object_effects[effect_name].called and
73                other.object_effects[effect_name].called):
74                new_potion.object_effects.pop(effect_name)
75                new_potion.effect.pop(effect_name)
76                delattr(new_potion, effect_name)
77        
72        return new_potion78        return new_potion
73    79    
74    def __mul__(self, other):80    def __mul__(self, other):
75        if self.called:81        if self.called:
76            raise TypeError('Potion is depleted.')82            raise TypeError('Potion is depleted.')
77        if self.part_of_something_bigger:83        if self.part_of_something_bigger:
78            raise TypeError('Potion is now part of something bigger than itself.')84            raise TypeError('Potion is now part of something bigger than itself.')
79        self.part_of_something_bigger = True85        self.part_of_something_bigger = True
80        new_potion = Potion(self.effect, self.duration)86        new_potion = Potion(self.effect, self.duration)
81        87        
82        for effect_name in self.object_effects:88        for effect_name in self.object_effects:
83            if self.object_effects[effect_name].called:89            if self.object_effects[effect_name].called:
84                new_potion.object_effects.pop(effect_name)90                new_potion.object_effects.pop(effect_name)
85                new_potion.effect.pop(effect_name)91                new_potion.effect.pop(effect_name)
86                delattr(new_potion, effect_name)92                delattr(new_potion, effect_name)
87                continue93                continue
88            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity94            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity
89            self.object_effects[effect_name].part_of_something_bigger = True95            self.object_effects[effect_name].part_of_something_bigger = True
90        for effect_name in new_potion.object_effects:96        for effect_name in new_potion.object_effects:
91            new_potion.object_effects[effect_name].intensity = int(round(new_potion.object_effects[effect_name].intensity * other, 0))97            new_potion.object_effects[effect_name].intensity = int(round(new_potion.object_effects[effect_name].intensity * other, 0))
92        return new_potion98        return new_potion
93    99    
94    def __sub__(self, other):100    def __sub__(self, other):
95        if self.called is True or other.called is True:101        if self.called is True or other.called is True:
96            raise TypeError('Potion is depleted.')102            raise TypeError('Potion is depleted.')
97        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:103        if self.part_of_something_bigger is True or other.part_of_something_bigger is True:
98            raise TypeError('Potion is now part of something bigger than itself.')104            raise TypeError('Potion is now part of something bigger than itself.')
99        self.part_of_something_bigger = True105        self.part_of_something_bigger = True
100        other.part_of_something_bigger = True106        other.part_of_something_bigger = True
101        new_potion = Potion(self.effect, self.duration)107        new_potion = Potion(self.effect, self.duration)
102        108        
103        for effect_name in self.object_effects:109        for effect_name in self.object_effects:
104            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity110            new_potion.object_effects[effect_name].intensity = self.object_effects[effect_name].intensity
105            self.object_effects[effect_name].part_of_something_bigger = True111            self.object_effects[effect_name].part_of_something_bigger = True
106            if effect_name in other.object_effects:112            if effect_name in other.object_effects:
107                other.object_effects[effect_name].part_of_something_bigger = True113                other.object_effects[effect_name].part_of_something_bigger = True
108114
109        for effect_name in other.object_effects:115        for effect_name in other.object_effects:
110            if effect_name not in  new_potion.object_effects.keys():116            if effect_name not in  new_potion.object_effects.keys():
111                raise TypeError('cannot remove non-existent effects')117                raise TypeError('cannot remove non-existent effects')
112            if other.object_effects[effect_name].called:118            if other.object_effects[effect_name].called:
113                continue119                continue
114            if new_potion.object_effects[effect_name].intensity > other.object_effects[effect_name].intensity:120            if new_potion.object_effects[effect_name].intensity > other.object_effects[effect_name].intensity:
115                new_potion.object_effects[effect_name].intensity -= other.object_effects[effect_name].intensity121                new_potion.object_effects[effect_name].intensity -= other.object_effects[effect_name].intensity
116            else:122            else:
117                new_potion.object_effects.pop(effect_name)123                new_potion.object_effects.pop(effect_name)
118                new_potion.effect.pop(effect_name)124                new_potion.effect.pop(effect_name)
119                delattr(new_potion, effect_name)125                delattr(new_potion, effect_name)
120                126                
121        for effect_name in self.object_effects:127        for effect_name in self.object_effects:
122            if self.object_effects[effect_name].called:128            if self.object_effects[effect_name].called:
123                new_potion.object_effects.pop(effect_name)129                new_potion.object_effects.pop(effect_name)
124                new_potion.effect.pop(effect_name)130                new_potion.effect.pop(effect_name)
125                delattr(new_potion, effect_name)131                delattr(new_potion, effect_name)
126        return new_potion132        return new_potion
127    133    
128    def __truediv__(self, other): 134    def __truediv__(self, other): 
129        potions = []135        potions = []
130        for _ in range(other):136        for _ in range(other):
131            potions.append(self * (1 / other))137            potions.append(self * (1 / other))
132            self.part_of_something_bigger = False138            self.part_of_something_bigger = False
133            for effect_name in self.object_effects:139            for effect_name in self.object_effects:
134                self.object_effects[effect_name].part_of_something_bigger = False140                self.object_effects[effect_name].part_of_something_bigger = False
135        141        
136        self.part_of_something_bigger = True142        self.part_of_something_bigger = True
137        for effect_name in self.object_effects:143        for effect_name in self.object_effects:
138            self.object_effects[effect_name].part_of_something_bigger = True144            self.object_effects[effect_name].part_of_something_bigger = True
139        return potions145        return potions
140    146    
141    def __eq__(self, other):147    def __eq__(self, other):
142        if self.called is True or other.called is True:148        if self.called is True or other.called is True:
143            raise TypeError('Potion is depleted.')149            raise TypeError('Potion is depleted.')
nn150        if self.effect != other.effect:
151            return False
144        for effect_name in self.object_effects:152        for effect_name in self.object_effects:
145            if (effect_name not in other.object_effects or 153            if (effect_name not in other.object_effects or 
146                    other.object_effects[effect_name].intensity != self.object_effects[effect_name].intensity):154                    other.object_effects[effect_name].intensity != self.object_effects[effect_name].intensity):
147                return False155                return False
148        156        
149        return True157        return True
150    158    
151    def __lt__(self, other):159    def __lt__(self, other):
152        if self.called is True or other.called is True:160        if self.called is True or other.called is True:
153            raise TypeError('Potion is depleted.')161            raise TypeError('Potion is depleted.')
154        self_intensity = 0162        self_intensity = 0
155        for effect in self.object_effects.values():163        for effect in self.object_effects.values():
156            self_intensity += effect.intensity164            self_intensity += effect.intensity
157        other_intensity = 0165        other_intensity = 0
158        for effect in other.object_effects.values():166        for effect in other.object_effects.values():
159            other_intensity += effect.intensity167            other_intensity += effect.intensity
160            168            
161        return self_intensity < other_intensity169        return self_intensity < other_intensity
162    170    
163    def __gt__(self, other):171    def __gt__(self, other):
t164        return not self < other and not self == othert172        return other < self
165    173    
166    174    
167class ГоспожатаПоХимия:175class ГоспожатаПоХимия:
168    176    
169    def apply(self, target, potion):177    def apply(self, target, potion):
170        if potion.called is True:178        if potion.called is True:
171            raise TypeError('Potion is depleted.')179            raise TypeError('Potion is depleted.')
172        180        
173        sorted_effects = list(sorted(potion.object_effects, key=lambda item: get_molecule_mass(item), reverse=True))181        sorted_effects = list(sorted(potion.object_effects, key=lambda item: get_molecule_mass(item), reverse=True))
174        for effect in sorted_effects:182        for effect in sorted_effects:
175            if potion.object_effects[effect].called is not True:183            if potion.object_effects[effect].called is not True:
176                potion.object_effects[effect](target)184                potion.object_effects[effect](target)
177        potion.called = True185        potion.called = True
178        186        
179    def tick(self):187    def tick(self):
180        pass188        pass
181        189        
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op