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

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

1 точки общо

2 успешни теста
18 неуспешни теста
Код

  1class Potion:
  2    def __init__(self, effects, duration,intensity=None):
  3        self.effects = effects 
  4        self.duration = duration
  5        self.is_depleted = False
  6       
  7        if intensity is not None:
  8            self.intensity = intensity
  9        else:
 10            self.intensity = {effect: 1 for effect in effects}
 11
 12    def apply_effect(self, effect_name, target):
 13        if self.is_depleted:
 14            raise TypeError("Potion is now part of something bigger than itself.")
 15        if effect_name not in self.effects:
 16            raise AttributeError("No such effect in this potion.")
 17
 18        effect = self.effects[effect_name]
 19        for _ in range(self.intensity[effect_name]):
 20            effect(target)
 21
 22        self.is_depleted = True
 23
 24    def __add__(self, other):
 25        if not isinstance(other, Potion):
 26            raise TypeError("Can only combine with another Potion.")
 27        combined_effects = self.effects.copy()
 28        for effect, func in other.effects.items():
 29            if effect in combined_effects:
 30                self.intensity[effect] += 1
 31            else:
 32                combined_effects[effect] = func
 33                self.intensity[effect] = 1
 34        combined_duration = max(self.duration, other.duration)
 35        return Potion(combined_effects, combined_duration)
 36
 37    def __mul__(self, factor):
 38        if not isinstance(factor, (int, float)):
 39            raise TypeError("Factor must be an integer or float.")
 40        if factor <= 0:
 41            raise ValueError("Factor must be positive.")
 42
 43        scaled_intensity = {effect: intensity * factor for effect, intensity in self.intensity.items()}
 44        return Potion(self.effects, self.duration, intensity=scaled_intensity)
 45
 46    def __sub__(self, other):
 47        if not isinstance(other, Potion):
 48            raise TypeError("Can only purify with another Potion.")
 49        purified_effects = {name: effect for name, effect in self.effects.items() if name not in other.effects}
 50        return Potion(purified_effects, self.duration)
 51
 52    def __truediv__(self, divisor):
 53        if not isinstance(divisor, int) or divisor <= 0:
 54            raise TypeError("Divisor must be a positive integer.")
 55
 56        new_effects = self.effects.copy()
 57        for effect in new_effects:
 58            self.intensity[effect] = max(1, self.intensity[effect] // divisor)
 59        return [Potion(new_effects, self.duration) for _ in range(divisor)]
 60
 61    def __eq__(self, other):
 62        return (self.effects, self.intensity) == (other.effects, other.intensity)
 63
 64    def __lt__(self, other):
 65        return sum(self.intensity.values()) < sum(other.intensity.values())
 66
 67    def __gt__(self, other):
 68        return sum(self.intensity.values()) > sum(other.intensity.values())
 69
 70class ГоспожатаПоХимия:
 71    def __init__(self):
 72        self.active_potions = {}
 73        self.applied_effects = {} 
 74
 75    def apply(self, target, potion):
 76        if potion.is_depleted:
 77            raise TypeError("Potion is depleted.")
 78
 79        for effect_name in sorted(potion.effects, key=lambda name: sum(ord(char) for char in name)):
 80            potion.apply_effect(effect_name, target)
 81            self.applied_effects.setdefault(target, []).append((effect_name, potion.effects[effect_name]))
 82
 83        self.active_potions[potion] = potion.duration
 84        potion.is_depleted = True
 85
 86    def tick(self):
 87        expired_potions = []
 88        for potion, duration in self.active_potions.items():
 89            self.active_potions[potion] -= 1
 90            if self.active_potions[potion] == 0:
 91                expired_potions.append(potion)
 92
 93        for potion in expired_potions:
 94            for target, effects in self.applied_effects.items():
 95                for effect_name, effect in effects:
 96                    if effect_name in potion.effects:
 97                        pass
 98
 99            del self.active_potions[potion]
100
101                

EE.F.EEEEEEEEEEEEEEE
======================================================================
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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute 'int_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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute '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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute 'float_attr_fun'

======================================================================
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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
File "/tmp/solution.py", line 80, in apply
potion.apply_effect(effect_name, target)
File "/tmp/solution.py", line 14, 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_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)
File "/tmp/solution.py", line 80, in apply
potion.apply_effect(effect_name, target)
File "/tmp/solution.py", line 14, 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_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)
File "/tmp/solution.py", line 80, in apply
potion.apply_effect(effect_name, target)
File "/tmp/solution.py", line 14, 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_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)
AttributeError: 'Potion' object has no attribute 'int_attr_fun'

======================================================================
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)
File "/tmp/solution.py", line 83, in apply
self.active_potions[potion] = potion.duration
TypeError: unhashable type: 'Potion'

======================================================================
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)
File "/tmp/solution.py", line 83, in apply
self.active_potions[potion] = potion.duration
TypeError: unhashable type: 'Potion'

======================================================================
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)
File "/tmp/solution.py", line 83, in apply
self.active_potions[potion] = potion.duration
TypeError: unhashable type: 'Potion'

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

======================================================================
FAIL: test_equal (test.TestPotionComparison)
Test equality of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 298, in test_equal
self.assertEqual(potion4, potion3)
AssertionError: <solution.Potion object at 0x7fec6e8770d0> != <solution.Potion object at 0x7fec6e876fe0>

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

FAILED (failures=1, errors=17)

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

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