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

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

0 точки общо

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

  1class Potion:
  2
  3    def __init__(self, effects, duration):
  4        self.effects = effects
  5        self.duration = duration
  6        self.called_effects = set()
  7        self.effects_intensity = dict.fromkeys(effects, 1)
  8    
  9    def __getattr__(self, name):
 10        if name in effects:
 11            if name in self.called_effects:
 12                raise TypeError("Effect is depleted.")
 13            self.called_effects.add(name)
 14            return self.execute_function(self.effects[name], self.effects_intensity[name])
 15
 16    def __add__(self, other):
 17        for effect in other.effects:
 18            if effect in self.effects:
 19                self.effects_intensity[effect] += other.effects_intensity[effect]
 20            else:
 21                self.effects[effect] = other.effects_intensity[effect]
 22
 23        self.duration = max(self.duration, other.duration)
 24        return self
 25    
 26    def __mul__(self, number):
 27        if number > 1:
 28            for effect in self.effects:
 29                self.effects_intensity[effect] *= number
 30            return self
 31        else:
 32            for effect in self.effects:
 33                self.effects_intensity[effect] = self.customized_round(self.effects_intensity[effect] * number)
 34            return self
 35    
 36    def execute_function(self, effect, intensity):
 37        def effect_of_potion(target):
 38            for element in range(intensity):
 39                effect(target)
 40        return effect_of_potion
 41    
 42    def customized_round(self, number):
 43        int_part = int(number)
 44        remaining_part = number - int_part
 45        if remaining_part < 0.5:
 46            return int_part
 47        else:
 48            return int_part + 1
 49        
 50    def __sub__(self, other):
 51        removed_effects = set()
 52        for effect in other.effects:
 53            if effect in self.effects:
 54                if self.effects_intensity[effect] <= other.effects_intensity[effect]:
 55                    self.effects_intensity.pop(effect)
 56                    removed_effects.add(effect)
 57                    #self.effects.pop(effect)
 58                else:
 59                    self.effects_intensity[effect] -= other.effects_intensity[effect]
 60            else:
 61                raise TypeError("You can`t remove an effect that doesn`t exist.")
 62            
 63        for element in removed_effects:
 64            self.effects.pop(element)
 65        return self
 66    
 67    def __eq__(self, other):
 68        different_effects = False
 69        for effect in other.effects:
 70            if effect not in self.effects:
 71                different_effects = True
 72            if self.effects_intensity[effect] != other.effects_intensity[effect]:
 73                different_effects = True
 74
 75        for effect in self.effects:
 76            if effect not in other.effects:
 77                different_effects = True
 78            if self.effects_intensity[effect] != other.effects_intensity[effect]:
 79                different_effects = True
 80
 81        if not different_effects:
 82            return True
 83        else:
 84            return False
 85        
 86    def __gt__(self, other):
 87        total_intensity_of_self = 0
 88        for effect in self.effects_intensity:
 89            total_intensity_of_self += self.effects_intensity[effect]
 90
 91        total_intensity_of_other = 0
 92        for effect in other.effects_intensity:
 93            total_intensity_of_other += other.effects_intensity[effect]
 94
 95        if total_intensity_of_self > total_intensity_of_other:
 96            return True
 97        else:
 98            return False
 99        
100    def __lt__(self, other):
101        total_intensity_of_self = 0
102        for effect in self.effects_intensity:
103            total_intensity_of_self += self.effects_intensity[effect]
104
105        total_intensity_of_other = 0
106        for effect in other.effects_intensity:
107            total_intensity_of_other += other.effects_intensity[effect]
108
109        if total_intensity_of_self < total_intensity_of_other:
110            return True
111        else:
112            return False
113
114class ГоспожатаПоХимия:
115    pass

EE.EEEEEEEEEEEEEEEEE
======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
ERROR: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 292, in test_equal
self.assertEqual(potion1 + potion2, potion3)
File "/usr/lib/python3.10/unittest/case.py", line 845, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib/python3.10/unittest/case.py", line 835, in _baseAssertEqual
if not first == second:
File "/tmp/solution.py", line 72, in __eq__
if self.effects_intensity[effect] != other.effects_intensity[effect]:
KeyError: 'float_attr_fun'

======================================================================
ERROR: test_superbness (test.TestPotionComparison)
Test superbness of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 325, in test_superbness
self.assertNotEqual(potion1, potion2)
File "/usr/lib/python3.10/unittest/case.py", line 851, in assertNotEqual
if not first != second:
File "/tmp/solution.py", line 72, in __eq__
if self.effects_intensity[effect] != other.effects_intensity[effect]:
KeyError: 'float_attr_fun'

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
ERROR: test_purification (test.TestPotionOperations)
Test purification of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 166, in test_purification
potion.float_attr_fun(self._target)
File "/tmp/solution.py", line 10, in __getattr__
if name in effects:
NameError: name 'effects' is not defined

======================================================================
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: unsupported operand type(s) for /: 'Potion' and 'int'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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 364, in test_applying_part_of_potion
potion.int_attr_fun(temp_target)
File "/tmp/solution.py", line 10, in __getattr__
if name in effects:
NameError: name 'effects' 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 424, in test_ticking_immutable
self._dimitrichka.apply(self._target, potion)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

======================================================================
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)
AttributeError: 'ГоспожатаПоХимия' object has no attribute 'apply'

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

FAILED (errors=19)

Дискусия
Георги Кунчев
06.12.2023 09:40

Нямам забележки по стила на кода. Жалко, че не ти е останало време да го завършиш.
История

f1class Potion:f1class Potion:
22
3    def __init__(self, effects, duration):3    def __init__(self, effects, duration):
4        self.effects = effects4        self.effects = effects
5        self.duration = duration5        self.duration = duration
6        self.called_effects = set()6        self.called_effects = set()
7        self.effects_intensity = dict.fromkeys(effects, 1)7        self.effects_intensity = dict.fromkeys(effects, 1)
8    8    
9    def __getattr__(self, name):9    def __getattr__(self, name):
10        if name in effects:10        if name in effects:
11            if name in self.called_effects:11            if name in self.called_effects:
12                raise TypeError("Effect is depleted.")12                raise TypeError("Effect is depleted.")
13            self.called_effects.add(name)13            self.called_effects.add(name)
14            return self.execute_function(self.effects[name], self.effects_intensity[name])14            return self.execute_function(self.effects[name], self.effects_intensity[name])
1515
16    def __add__(self, other):16    def __add__(self, other):
17        for effect in other.effects:17        for effect in other.effects:
18            if effect in self.effects:18            if effect in self.effects:
19                self.effects_intensity[effect] += other.effects_intensity[effect]19                self.effects_intensity[effect] += other.effects_intensity[effect]
20            else:20            else:
21                self.effects[effect] = other.effects_intensity[effect]21                self.effects[effect] = other.effects_intensity[effect]
2222
23        self.duration = max(self.duration, other.duration)23        self.duration = max(self.duration, other.duration)
24        return self24        return self
25    25    
26    def __mul__(self, number):26    def __mul__(self, number):
27        if number > 1:27        if number > 1:
28            for effect in self.effects:28            for effect in self.effects:
29                self.effects_intensity[effect] *= number29                self.effects_intensity[effect] *= number
30            return self30            return self
31        else:31        else:
32            for effect in self.effects:32            for effect in self.effects:
33                self.effects_intensity[effect] = self.customized_round(self.effects_intensity[effect] * number)33                self.effects_intensity[effect] = self.customized_round(self.effects_intensity[effect] * number)
34            return self34            return self
35    35    
36    def execute_function(self, effect, intensity):36    def execute_function(self, effect, intensity):
37        def effect_of_potion(target):37        def effect_of_potion(target):
38            for element in range(intensity):38            for element in range(intensity):
39                effect(target)39                effect(target)
40        return effect_of_potion40        return effect_of_potion
41    41    
42    def customized_round(self, number):42    def customized_round(self, number):
43        int_part = int(number)43        int_part = int(number)
44        remaining_part = number - int_part44        remaining_part = number - int_part
45        if remaining_part < 0.5:45        if remaining_part < 0.5:
46            return int_part46            return int_part
47        else:47        else:
48            return int_part + 148            return int_part + 1
49        49        
50    def __sub__(self, other):50    def __sub__(self, other):
51        removed_effects = set()51        removed_effects = set()
52        for effect in other.effects:52        for effect in other.effects:
53            if effect in self.effects:53            if effect in self.effects:
54                if self.effects_intensity[effect] <= other.effects_intensity[effect]:54                if self.effects_intensity[effect] <= other.effects_intensity[effect]:
55                    self.effects_intensity.pop(effect)55                    self.effects_intensity.pop(effect)
56                    removed_effects.add(effect)56                    removed_effects.add(effect)
57                    #self.effects.pop(effect)57                    #self.effects.pop(effect)
58                else:58                else:
59                    self.effects_intensity[effect] -= other.effects_intensity[effect]59                    self.effects_intensity[effect] -= other.effects_intensity[effect]
60            else:60            else:
61                raise TypeError("You can`t remove an effect that doesn`t exist.")61                raise TypeError("You can`t remove an effect that doesn`t exist.")
62            62            
63        for element in removed_effects:63        for element in removed_effects:
64            self.effects.pop(element)64            self.effects.pop(element)
65        return self65        return self
66    66    
67    def __eq__(self, other):67    def __eq__(self, other):
68        different_effects = False68        different_effects = False
69        for effect in other.effects:69        for effect in other.effects:
70            if effect not in self.effects:70            if effect not in self.effects:
71                different_effects = True71                different_effects = True
72            if self.effects_intensity[effect] != other.effects_intensity[effect]:72            if self.effects_intensity[effect] != other.effects_intensity[effect]:
73                different_effects = True73                different_effects = True
7474
75        for effect in self.effects:75        for effect in self.effects:
76            if effect not in other.effects:76            if effect not in other.effects:
77                different_effects = True77                different_effects = True
78            if self.effects_intensity[effect] != other.effects_intensity[effect]:78            if self.effects_intensity[effect] != other.effects_intensity[effect]:
79                different_effects = True79                different_effects = True
8080
81        if not different_effects:81        if not different_effects:
82            return True82            return True
83        else:83        else:
84            return False84            return False
85        85        
86    def __gt__(self, other):86    def __gt__(self, other):
87        total_intensity_of_self = 087        total_intensity_of_self = 0
88        for effect in self.effects_intensity:88        for effect in self.effects_intensity:
89            total_intensity_of_self += self.effects_intensity[effect]89            total_intensity_of_self += self.effects_intensity[effect]
9090
91        total_intensity_of_other = 091        total_intensity_of_other = 0
92        for effect in other.effects_intensity:92        for effect in other.effects_intensity:
93            total_intensity_of_other += other.effects_intensity[effect]93            total_intensity_of_other += other.effects_intensity[effect]
9494
95        if total_intensity_of_self > total_intensity_of_other:95        if total_intensity_of_self > total_intensity_of_other:
96            return True96            return True
97        else:97        else:
98            return False98            return False
99        99        
100    def __lt__(self, other):100    def __lt__(self, other):
101        total_intensity_of_self = 0101        total_intensity_of_self = 0
102        for effect in self.effects_intensity:102        for effect in self.effects_intensity:
103            total_intensity_of_self += self.effects_intensity[effect]103            total_intensity_of_self += self.effects_intensity[effect]
104104
105        total_intensity_of_other = 0105        total_intensity_of_other = 0
106        for effect in other.effects_intensity:106        for effect in other.effects_intensity:
107            total_intensity_of_other += other.effects_intensity[effect]107            total_intensity_of_other += other.effects_intensity[effect]
108108
109        if total_intensity_of_self < total_intensity_of_other:109        if total_intensity_of_self < total_intensity_of_other:
110            return True110            return True
111        else:111        else:
112            return False112            return False
tt113 
114class ГоспожатаПоХимия:
115    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op