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

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

0 точки общо

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

 1class Potion:
 2    def __init__(self, effects, duration):
 3        self.duration = duration
 4        self.effects = effects
 5        self.apply_effects = set()
 6
 7    def __getattr__(self, attr):
 8        if attr in self.effects:
 9            if attr in self.apply_effects:
10                raise TypeError("Effect is depleted.")
11            self.apply_effects.add(attr)
12            return self.apply_effects
13        return
14
15    def __add__(self, other):
16        combined_effects = self.effects.copy()
17        for effect, intensity in other.effects.items():
18            if effect in combined_effects:
19                combined_effects[effect] += intensity
20            else:
21                combined_effects[effect] = intensity
22
23        combined_duration = max(self.duration, other.duration)
24
25        return Potion(combined_effects, combined_duration)
26
27    def __mul__(self, other):
28        if isinstance(other, int) and other >= 0:
29            return Potion(self.intensity * other)
30        pass
31
32    def dilute_potion(original_intensity, dilution_factor):
33        if 0 <= dilution_factor <= 1:
34            diluted_intensity = original_intensity * dilution_factor
35
36        if diluted_intensity - int(diluted_intensity) > 0.5:
37            diluted_intensity = int(diluted_intensity) + 1
38        diluted_intensity = int(diluted_intensity)
39
40        return diluted_intensity
41
42    def apply(self):
43        if self.used:
44            raise TypeError(
45                "Potion is now part of something bigger than itself.")
46
47
48class ГоспожатаПоХимия:
49    def apply(target, potion):
50        pass
51
52    def tick():
53        pass

EE.FEEEEEEEEEEEEEEEE
======================================================================
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)
TypeError: 'set' object is not callable

======================================================================
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)
TypeError: 'set' object is not callable

======================================================================
ERROR: test_superbness (test.TestPotionComparison)
Test superbness of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 316, in test_superbness
potion2 = Potion({'int_attr_fun': int_attr_fun}, duration=1) * 2
File "/tmp/solution.py", line 29, in __mul__
return Potion(self.intensity * other)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

======================================================================
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)
TypeError: 'set' object is not callable

======================================================================
ERROR: test_combination_with_overlap (test.TestPotionOperations)
Test combining potions with overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 103, in test_combination_with_overlap
potion = potion1 + potion2
File "/tmp/solution.py", line 19, in __add__
combined_effects[effect] += intensity
TypeError: unsupported operand type(s) for +=: 'function' and 'function'

======================================================================
ERROR: test_deprecation (test.TestPotionOperations)
Test deprecation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 256, in test_deprecation
potion = potion1 + potion2
File "/tmp/solution.py", line 19, in __add__
combined_effects[effect] += intensity
TypeError: unsupported operand type(s) for +=: 'function' and 'function'

======================================================================
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: 'NoneType' 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 110, in test_potentiation
potion = potion * 3
File "/tmp/solution.py", line 29, in __mul__
return Potion(self.intensity * other)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

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

======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 212, in test_separation
potion = Potion({'int_attr_fun': int_attr_fun}, duration=1) * 9
File "/tmp/solution.py", line 29, in __mul__
return Potion(self.intensity * other)
TypeError: unsupported operand type(s) for *: 'NoneType' 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)
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)
TypeError: 'set' object is not callable

======================================================================
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 423, in test_ticking_immutable
potion = potion1 + potion2 # Excepted duration is 2 with intensity of 2
File "/tmp/solution.py", line 19, in __add__
combined_effects[effect] += intensity
TypeError: unsupported operand type(s) for +=: 'function' and 'function'

======================================================================
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 479, in test_ticking_multiple_targets
potion1 = Potion({'int_attr_fun': int_attr_fun}, duration=1) * 2
File "/tmp/solution.py", line 29, in __mul__
return Potion(self.intensity * other)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

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

======================================================================
FAIL: 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)
AssertionError: <solution.Potion object at 0x7fb09d547100> != <solution.Potion object at 0x7fb09d5470d0>

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

FAILED (failures=1, errors=18)

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

f1class Potion:f1class Potion:
2    def __init__(self, effects, duration):2    def __init__(self, effects, duration):
3        self.duration = duration3        self.duration = duration
4        self.effects = effects4        self.effects = effects
5        self.apply_effects = set()5        self.apply_effects = set()
66
7    def __getattr__(self, attr):7    def __getattr__(self, attr):
8        if attr in self.effects:8        if attr in self.effects:
9            if attr in self.apply_effects:9            if attr in self.apply_effects:
10                raise TypeError("Effect is depleted.")10                raise TypeError("Effect is depleted.")
11            self.apply_effects.add(attr)11            self.apply_effects.add(attr)
12            return self.apply_effects12            return self.apply_effects
13        return13        return
1414
15    def __add__(self, other):15    def __add__(self, other):
16        combined_effects = self.effects.copy()16        combined_effects = self.effects.copy()
17        for effect, intensity in other.effects.items():17        for effect, intensity in other.effects.items():
18            if effect in combined_effects:18            if effect in combined_effects:
19                combined_effects[effect] += intensity19                combined_effects[effect] += intensity
20            else:20            else:
21                combined_effects[effect] = intensity21                combined_effects[effect] = intensity
2222
23        combined_duration = max(self.duration, other.duration)23        combined_duration = max(self.duration, other.duration)
2424
25        return Potion(combined_effects, combined_duration)25        return Potion(combined_effects, combined_duration)
2626
27    def __mul__(self, other):27    def __mul__(self, other):
28        if isinstance(other, int) and other >= 0:28        if isinstance(other, int) and other >= 0:
29            return Potion(self.intensity * other)29            return Potion(self.intensity * other)
30        pass30        pass
3131
32    def dilute_potion(original_intensity, dilution_factor):32    def dilute_potion(original_intensity, dilution_factor):
33        if 0 <= dilution_factor <= 1:33        if 0 <= dilution_factor <= 1:
34            diluted_intensity = original_intensity * dilution_factor34            diluted_intensity = original_intensity * dilution_factor
3535
36        if diluted_intensity - int(diluted_intensity) > 0.5:36        if diluted_intensity - int(diluted_intensity) > 0.5:
37            diluted_intensity = int(diluted_intensity) + 137            diluted_intensity = int(diluted_intensity) + 1
38        diluted_intensity = int(diluted_intensity)38        diluted_intensity = int(diluted_intensity)
3939
40        return diluted_intensity40        return diluted_intensity
4141
42    def apply(self):42    def apply(self):
43        if self.used:43        if self.used:
44            raise TypeError(44            raise TypeError(
45                "Potion is now part of something bigger than itself.")45                "Potion is now part of something bigger than itself.")
tt46 
47 
48class ГоспожатаПоХимия:
49    def apply(target, potion):
50        pass
51 
52    def tick():
53        pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op