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

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

2 точки общо

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

  1class Potion:
  2    def __init__(self, effects, duration):
  3        self.effects=effects
  4        self.duration=duration
  5        self.intensities = {}
  6        for effect in effects:
  7            self.intensities[effect] = 1.0
  8        self.used = False
  9        
 10    def __add__(self, rhs):
 11        if self.used or rhs.used:
 12            raise TypeError("Potion is depleted.")
 13        new_effects = self.effects.copy()
 14        new_intensities = self.intensities.copy()
 15        for effect in rhs.effects:
 16            if effect in new_effects:
 17                new_intensities[effect] += 1.0
 18            else:
 19                new_effects[effect]=rhs.effects[effect]
 20                new_intensities[effect] = 1.0
 21                
 22           
 23        new_duration = max(self.duration, rhs.duration)
 24        
 25        new_potion = Potion(new_effects, new_duration)
 26        new_potion.intensities = new_intensities
 27        self.used = True
 28        rhs.used = True
 29        return new_potion
 30    
 31    def apply_effect(self, target, effect_name):
 32        if self.used:
 33            raise TypeError("Potion is now part of something bigger than itself.")
 34        if effect_name in self.effects:
 35            effect_func = self.effects[effect_name]
 36            effect_intensity = self.intensities[effect_name]
 37            for _ in range(int(self.intensities[effect_name])):
 38                effect_func(target)
 39            self.used = True
 40        else:
 41            raise ValueError(f"Effect '{effect_name}' not found.")
 42    
 43    def __getattr__(self, effect_name):
 44        if effect_name in self.effects:
 45            if not self.used:
 46                self.used = True
 47                return lambda target: self.apply_effect(target, effect_name)
 48            else:
 49                raise TypeError("Potion is now part of something bigger than itself.")
 50        else:
 51            raise AttributeError(f"'Potion' object has no attribute '{effect_name}'")
 52        
 53    def __mul__(self, value):
 54        if self.used:
 55            raise TypeError("Potion is depleted.")
 56        new_potion = Potion(self.effects, self.duration)
 57        if(value < 0):
 58            raise ValueError("Value must be a positive number!")
 59        if value > 0 and value < 1:
 60            for effect in self.effects:
 61                new_potion.intensities[effect] = round(self.intensities[effect] * value)    
 62        else:
 63            for effect in self.effects:
 64                new_potion.intensities[effect] *= value
 65        self.used = True
 66        return new_potion
 67    
 68    def __sub__(self, other):
 69        if self.used or other.used:
 70            raise TypeError("Potion is depleted.")
 71        if set(other.effects.keys()) - set(self.effects.keys()):
 72            raise TypeError("Second potion effects don't match the effects of the first one!")
 73
 74        new_effects = self.effects.copy()
 75        new_intensities = self.intensities.copy()
 76        new_potion = Potion(new_effects, self.duration)  
 77
 78        for effect in self.effects.copy():
 79            if effect in other.effects:
 80                if self.intensities[effect] > other.intensities[effect]:
 81                    new_intensities[effect] = self.intensities[effect] - other.intensities[effect]
 82                else:
 83                    new_effects.pop(effect)
 84                    new_intensities.pop(effect)
 85
 86        new_potion.intensities = new_intensities
 87        self.used = True
 88        other.used = True
 89        return new_potion
 90    
 91    def __truediv__(self,value):
 92        if self.used:
 93            raise TypeError("Potion is depleted.")
 94        temp = []
 95        for _ in range(value):
 96            new_potion = Potion(self.effects, self.duration)
 97            for effect in self.effects:
 98                new_potion.intensities[effect] = round(self.intensities[effect] / value)
 99            temp.append(new_potion)
100        self.used = True
101        return tuple(temp)
102    
103    def __eq__(self, other):
104        return self.effects == other.effects and self.intensities == other.intensities
105    
106    def __lt__(self,other):
107        sum1 = 0
108        sum2 = 0
109        for effect in self.effects:
110            sum1 += self.intensities[effect]
111        for effect in other.effects:
112            sum2 += other.intensities[effect]
113        return sum2 > sum1
114    
115    def __gt__(self,other):
116        sum1 = 0
117        sum2 = 0
118        for effect in self.effects:
119            sum1 += self.intensities[effect]
120        for effect in other.effects:
121            sum2 += other.intensities[effect]
122        return sum2 < sum1
123
124
125class ГоспожатаПоХимия:
126    def __init__(self):
127        pass
128
129    def apply(target, potion):
130        if potion.used:
131            raise TypeError("Potion is depleted.")
132        for effect_name in potion.effects.keys():
133            potion.apply_effect(target,effect_name)

EE...EE.EEEEEEEEEEEE
======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 214, in test_separation
potion1.int_attr_fun(self._target)
File "/tmp/solution.py", line 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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 47, in <lambda>
return lambda target: self.apply_effect(target, effect_name)
File "/tmp/solution.py", line 33, in apply_effect
raise TypeError("Potion is now part of something bigger than itself.")
TypeError: Potion is now part of something bigger than itself.

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

======================================================================
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)
TypeError: ГоспожатаПоХимия.apply() takes 2 positional arguments but 3 were given

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

FAILED (errors=16)

Дискусия
История
Това решение има само една версия.