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

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

0 точки общо

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

  1def add_effects(current_potion, old_potion):
  2    for effect in old_potion.effects:
  3        if effect not in current_potion.effects:
  4            current_potion.effects[effect] = old_potion.effects[effect]
  5
  6    for effect in old_potion.effects_intensity:
  7        if effect not in current_potion.effects_intensity:
  8            current_potion.effects_intensity[effect] = old_potion.effects_intensity[effect]
  9        else:
 10            current_potion.effects_intensity[effect] += old_potion.effects_intensity[effect]
 11
 12def delete_effects(potion):
 13    potion.effects = {}
 14    potion.effects_intensity = {}
 15
 16def multiply_effects_intensity(potion, num):
 17    for effect in potion.effects_intensity:
 18        potion.effects_intensity[effect] *= num
 19
 20def get_sum_intensity(potion):
 21    sum = 0
 22    for effect in potion.effects_intensity:
 23        sum += potion.effects_intensity[effect]
 24
 25    return sum
 26
 27def check_effects(left_potion, right_potion):
 28    for effect in right_potion.effects:
 29        if effect not in left_potion.effects:
 30            return False
 31        
 32    return True
 33
 34def calculate_intensity(left_potion, right_potion):
 35    for effect in left_potion.effects:
 36        if effect in right_potion.effects:
 37            if right_potion.effects_intensity[effect] >= left_potion.effects_intensity[effect]:
 38                left_potion.effects_intensity[effect] = 0
 39            else:
 40                left_potion.effects_intensity[effect] -= right_potion.effects_intensity[effect]
 41
 42def calculate_ascii_sum(potion):
 43    ascii_dict = {}
 44    for effect in potion.effects:
 45        ascii_dict[effect] = sum(ord(symbol) for symbol in effect)
 46
 47    return ascii_dict
 48
 49class Potion:
 50    effects = {}
 51    effects_intensity = {}
 52    duration = 0
 53
 54    def __init__(self, effects, duration):
 55        self.effects = effects
 56        self.duration = duration
 57        for effect in self.effects:
 58            self.effects_intensity[effect] = 1 
 59
 60    def __getattr__(self, name):
 61        if self.effects[name] not in self.executed_effects:
 62            return self.effects[name]
 63        elif self.effects == {}:
 64            raise TypeError('Potion is now part of something bigger than itself.')
 65        else:
 66            raise TypeError('Effect is depleted.')
 67        
 68
 69    def __add__(self, other):
 70        new_potion = Potion({}, (self.duration if self.duration > other.duration else other.duration))
 71        add_effects(new_potion, self)
 72        add_effects(new_potion, other)
 73        delete_effects(self)
 74        delete_effects(other)
 75        return new_potion
 76
 77    def __mul__(self, num):
 78        new_potion = Potion(self.effects, self.duration)
 79        if isinstance(num, int):
 80            multiply_effects_intensity(new_potion, num)
 81        elif isinstance(num, float):
 82            multiply_effects_intensity(new_potion, round(num))
 83
 84        delete_effects(self)
 85        return new_potion
 86
 87    def __sub__(self, other):
 88        new_potion = Potion({}, self.duration)
 89
 90        if not check_effects(self, other):
 91            raise TypeError('Missing potion')
 92        
 93        calculate_intensity(self, other)
 94        
 95        add_effects(new_potion, self)
 96        for effect in new_potion.effects:
 97            if effect in other.effects:
 98                new_potion.effects.pop(effect)
 99                new_potion.effects_intensity.pop(effect)
100
101        delete_effects(self)
102        delete_effects(other)
103        return new_potion
104
105    def __truediv__(self, num):
106        new_potion = Potion({}, self.duration)
107        for effect in self.effects:
108            new_potion.effects[effect] = self.effects[effect]
109            new_potion.effects_intensity[effect] = self.effects_intensity[effect] / round(num)
110
111        delete_effects(self)
112        return new_potion
113
114    def __eq__(self, other):
115        if self.effects == other.effects:
116            for effect in self.effects:
117                if self.effects_intensity[effect] != other.effects_intensity[effect]:
118                    return False
119                
120            return True
121        else:
122            return False
123
124    def __gt__(self, other):
125        return get_sum_intensity(self) > get_sum_intensity(other)
126
127    def __lt__(self, other):
128        return get_sum_intensity(self) < get_sum_intensity(other)
129        
130
131
132class TodorkaValkova:
133
134    def __init__(self):
135        self.executed_effects = []
136        self.executed_potions = []
137        self.last_potion = None
138        self.old_target = None
139        self.modified_target = None
140
141    def apply(self, target, potion):
142        if potion in self.executed_potions:
143            raise TypeError('Potion is depleted')
144        
145        self.executed_potions.append(potion) 
146        self.modified_target = target
147        self.old_target = target.__dict__.copy()
148
149        ascii_sums = dict(sorted(calculate_ascii_sum(potion).items(), key=lambda x: x[1], reverse=True))
150
151        for effect in ascii_sums:
152            if effect in self.executed_effects:
153                raise TypeError('Effect is depleted')
154            
155            self.targets_and_potions[target] = potion
156            
157            self.executed_effects.append(effect)
158            self.last_potion = potion
159
160            for i in range(0,potion.effects_intensity[effect]):
161                potion.effects[effect](target)
162    
163    def tick(self):
164        if self.last_potion is not None:
165            if self.last_potion.duration > 0:
166                self.last_potion.duration =- 1
167            else:
168                if self.modified_target is not None and self.old_target is not None:
169                    self.modified_target.__dict__ = self.old_target

EE.FFEEEEEEEEEEEEEEE
======================================================================
ERROR: test_applying (test.TestBasicPotion)
Test applying a potion to a target.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 63, in test_applying
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 76, in test_depletion
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_combination_no_overlap (test.TestPotionOperations)
Test combining potions with no overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 94, in test_combination_no_overlap
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_combination_with_overlap (test.TestPotionOperations)
Test combining potions with overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 104, in test_combination_with_overlap
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 258, in test_deprecation
potion1.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'int_attr_fun'

======================================================================
ERROR: test_dilution (test.TestPotionOperations)
Test dilution of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 119, in test_dilution
half_potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_potentiation (test.TestPotionOperations)
Test potentiation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 111, in test_potentiation
potion.int_attr_fun(self._target)
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
File "/tmp/solution.py", line 61, in __getattr__
if self.effects[name] not in self.executed_effects:
KeyError: 'executed_effects'

======================================================================
ERROR: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 165, in test_purification
potion = potion1 - potion2
File "/tmp/solution.py", line 96, in __sub__
for effect in new_potion.effects:
RuntimeError: dictionary changed size during iteration

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

======================================================================
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 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_normal_case (test.TestГоспожатаПоХимия)
Test applying a normal potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
ERROR: test_applying_part_of_potion (test.TestГоспожатаПоХимия)
Test applying only a part of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
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 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
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 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
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 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
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 341, in setUp
self._dimitrichka = ГоспожатаПоХимия()
NameError: name 'ГоспожатаПоХимия' is not defined

======================================================================
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 0x7fbd0680e890> == <solution.Potion object at 0x7fbd0680e740>

======================================================================
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 0x7fbd0680e890> not less than <solution.Potion object at 0x7fbd0680f460>

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

FAILED (failures=2, errors=17)

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

f1def add_effects(current_potion, old_potion):f1def add_effects(current_potion, old_potion):
2    for effect in old_potion.effects:2    for effect in old_potion.effects:
3        if effect not in current_potion.effects:3        if effect not in current_potion.effects:
4            current_potion.effects[effect] = old_potion.effects[effect]4            current_potion.effects[effect] = old_potion.effects[effect]
55
6    for effect in old_potion.effects_intensity:6    for effect in old_potion.effects_intensity:
7        if effect not in current_potion.effects_intensity:7        if effect not in current_potion.effects_intensity:
8            current_potion.effects_intensity[effect] = old_potion.effects_intensity[effect]8            current_potion.effects_intensity[effect] = old_potion.effects_intensity[effect]
9        else:9        else:
10            current_potion.effects_intensity[effect] += old_potion.effects_intensity[effect]10            current_potion.effects_intensity[effect] += old_potion.effects_intensity[effect]
1111
12def delete_effects(potion):12def delete_effects(potion):
13    potion.effects = {}13    potion.effects = {}
14    potion.effects_intensity = {}14    potion.effects_intensity = {}
1515
16def multiply_effects_intensity(potion, num):16def multiply_effects_intensity(potion, num):
17    for effect in potion.effects_intensity:17    for effect in potion.effects_intensity:
18        potion.effects_intensity[effect] *= num18        potion.effects_intensity[effect] *= num
1919
20def get_sum_intensity(potion):20def get_sum_intensity(potion):
21    sum = 021    sum = 0
22    for effect in potion.effects_intensity:22    for effect in potion.effects_intensity:
23        sum += potion.effects_intensity[effect]23        sum += potion.effects_intensity[effect]
2424
25    return sum25    return sum
2626
27def check_effects(left_potion, right_potion):27def check_effects(left_potion, right_potion):
28    for effect in right_potion.effects:28    for effect in right_potion.effects:
29        if effect not in left_potion.effects:29        if effect not in left_potion.effects:
30            return False30            return False
31        31        
32    return True32    return True
3333
34def calculate_intensity(left_potion, right_potion):34def calculate_intensity(left_potion, right_potion):
35    for effect in left_potion.effects:35    for effect in left_potion.effects:
36        if effect in right_potion.effects:36        if effect in right_potion.effects:
37            if right_potion.effects_intensity[effect] >= left_potion.effects_intensity[effect]:37            if right_potion.effects_intensity[effect] >= left_potion.effects_intensity[effect]:
38                left_potion.effects_intensity[effect] = 038                left_potion.effects_intensity[effect] = 0
39            else:39            else:
40                left_potion.effects_intensity[effect] -= right_potion.effects_intensity[effect]40                left_potion.effects_intensity[effect] -= right_potion.effects_intensity[effect]
4141
42def calculate_ascii_sum(potion):42def calculate_ascii_sum(potion):
43    ascii_dict = {}43    ascii_dict = {}
44    for effect in potion.effects:44    for effect in potion.effects:
45        ascii_dict[effect] = sum(ord(symbol) for symbol in effect)45        ascii_dict[effect] = sum(ord(symbol) for symbol in effect)
4646
47    return ascii_dict47    return ascii_dict
4848
49class Potion:49class Potion:
50    effects = {}50    effects = {}
51    effects_intensity = {}51    effects_intensity = {}
52    duration = 052    duration = 0
5353
54    def __init__(self, effects, duration):54    def __init__(self, effects, duration):
55        self.effects = effects55        self.effects = effects
56        self.duration = duration56        self.duration = duration
57        for effect in self.effects:57        for effect in self.effects:
58            self.effects_intensity[effect] = 1 58            self.effects_intensity[effect] = 1 
5959
60    def __getattr__(self, name):60    def __getattr__(self, name):
61        if self.effects[name] not in self.executed_effects:61        if self.effects[name] not in self.executed_effects:
62            return self.effects[name]62            return self.effects[name]
63        elif self.effects == {}:63        elif self.effects == {}:
64            raise TypeError('Potion is now part of something bigger than itself.')64            raise TypeError('Potion is now part of something bigger than itself.')
65        else:65        else:
66            raise TypeError('Effect is depleted.')66            raise TypeError('Effect is depleted.')
67        67        
6868
69    def __add__(self, other):69    def __add__(self, other):
70        new_potion = Potion({}, (self.duration if self.duration > other.duration else other.duration))70        new_potion = Potion({}, (self.duration if self.duration > other.duration else other.duration))
71        add_effects(new_potion, self)71        add_effects(new_potion, self)
72        add_effects(new_potion, other)72        add_effects(new_potion, other)
73        delete_effects(self)73        delete_effects(self)
74        delete_effects(other)74        delete_effects(other)
75        return new_potion75        return new_potion
7676
77    def __mul__(self, num):77    def __mul__(self, num):
78        new_potion = Potion(self.effects, self.duration)78        new_potion = Potion(self.effects, self.duration)
79        if isinstance(num, int):79        if isinstance(num, int):
80            multiply_effects_intensity(new_potion, num)80            multiply_effects_intensity(new_potion, num)
81        elif isinstance(num, float):81        elif isinstance(num, float):
82            multiply_effects_intensity(new_potion, round(num))82            multiply_effects_intensity(new_potion, round(num))
8383
84        delete_effects(self)84        delete_effects(self)
85        return new_potion85        return new_potion
8686
87    def __sub__(self, other):87    def __sub__(self, other):
88        new_potion = Potion({}, self.duration)88        new_potion = Potion({}, self.duration)
8989
90        if not check_effects(self, other):90        if not check_effects(self, other):
91            raise TypeError('Missing potion')91            raise TypeError('Missing potion')
92        92        
93        calculate_intensity(self, other)93        calculate_intensity(self, other)
94        94        
95        add_effects(new_potion, self)95        add_effects(new_potion, self)
96        for effect in new_potion.effects:96        for effect in new_potion.effects:
97            if effect in other.effects:97            if effect in other.effects:
98                new_potion.effects.pop(effect)98                new_potion.effects.pop(effect)
99                new_potion.effects_intensity.pop(effect)99                new_potion.effects_intensity.pop(effect)
100100
101        delete_effects(self)101        delete_effects(self)
102        delete_effects(other)102        delete_effects(other)
103        return new_potion103        return new_potion
104104
105    def __truediv__(self, num):105    def __truediv__(self, num):
106        new_potion = Potion({}, self.duration)106        new_potion = Potion({}, self.duration)
107        for effect in self.effects:107        for effect in self.effects:
108            new_potion.effects[effect] = self.effects[effect]108            new_potion.effects[effect] = self.effects[effect]
109            new_potion.effects_intensity[effect] = self.effects_intensity[effect] / round(num)109            new_potion.effects_intensity[effect] = self.effects_intensity[effect] / round(num)
110110
111        delete_effects(self)111        delete_effects(self)
112        return new_potion112        return new_potion
113113
114    def __eq__(self, other):114    def __eq__(self, other):
115        if self.effects == other.effects:115        if self.effects == other.effects:
116            for effect in self.effects:116            for effect in self.effects:
117                if self.effects_intensity[effect] != other.effects_intensity[effect]:117                if self.effects_intensity[effect] != other.effects_intensity[effect]:
118                    return False118                    return False
119                119                
120            return True120            return True
121        else:121        else:
122            return False122            return False
123123
124    def __gt__(self, other):124    def __gt__(self, other):
125        return get_sum_intensity(self) > get_sum_intensity(other)125        return get_sum_intensity(self) > get_sum_intensity(other)
126126
127    def __lt__(self, other):127    def __lt__(self, other):
128        return get_sum_intensity(self) < get_sum_intensity(other)128        return get_sum_intensity(self) < get_sum_intensity(other)
129        129        
130130
131131
132class TodorkaValkova:132class TodorkaValkova:
133133
134    def __init__(self):134    def __init__(self):
135        self.executed_effects = []135        self.executed_effects = []
136        self.executed_potions = []136        self.executed_potions = []
137        self.last_potion = None137        self.last_potion = None
138        self.old_target = None138        self.old_target = None
139        self.modified_target = None139        self.modified_target = None
140140
141    def apply(self, target, potion):141    def apply(self, target, potion):
142        if potion in self.executed_potions:142        if potion in self.executed_potions:
143            raise TypeError('Potion is depleted')143            raise TypeError('Potion is depleted')
144        144        
145        self.executed_potions.append(potion) 145        self.executed_potions.append(potion) 
146        self.modified_target = target146        self.modified_target = target
147        self.old_target = target.__dict__.copy()147        self.old_target = target.__dict__.copy()
148148
149        ascii_sums = dict(sorted(calculate_ascii_sum(potion).items(), key=lambda x: x[1], reverse=True))149        ascii_sums = dict(sorted(calculate_ascii_sum(potion).items(), key=lambda x: x[1], reverse=True))
150150
151        for effect in ascii_sums:151        for effect in ascii_sums:
152            if effect in self.executed_effects:152            if effect in self.executed_effects:
t153                raise TypeError('Potion is depleted')t153                raise TypeError('Effect is depleted')
154            154            
155            self.targets_and_potions[target] = potion155            self.targets_and_potions[target] = potion
156            156            
157            self.executed_effects.append(effect)157            self.executed_effects.append(effect)
158            self.last_potion = potion158            self.last_potion = potion
159159
160            for i in range(0,potion.effects_intensity[effect]):160            for i in range(0,potion.effects_intensity[effect]):
161                potion.effects[effect](target)161                potion.effects[effect](target)
162    162    
163    def tick(self):163    def tick(self):
164        if self.last_potion is not None:164        if self.last_potion is not None:
165            if self.last_potion.duration > 0:165            if self.last_potion.duration > 0:
166                self.last_potion.duration =- 1166                self.last_potion.duration =- 1
167            else:167            else:
168                if self.modified_target is not None and self.old_target is not None:168                if self.modified_target is not None and self.old_target is not None:
169                    self.modified_target.__dict__ = self.old_target169                    self.modified_target.__dict__ = self.old_target
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op