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

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

1 точки общо

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

 1import math
 2
 3
 4def composite_function(f, g):
 5    return lambda x: f(g(x))
 6
 7
 8class Potion:
 9    def __init__(self, effects, duration=2):
10        self.effects = effects
11        self.duration = duration
12        self._used = False
13
14        for effect in effects.keys():
15            setattr(self, effect, effects[effect])
16
17    def __add__(self, other):
18        for other_key in other.effects.keys():
19            if other_key not in self.effects.keys():
20                self.effects[other_key] = other.effects[other_key]
21
22        self.duration += other.duration
23
24        for other_effect in other.effects.keys():
25            if other_effect in self.effects.keys():
26                self.effects.update(
27                    other_effect,
28                    composite_function(
29                        self.effects[other_effect], self.effects[other_effect]
30                    ),
31                )
32
33    def __mul__(self, n):
34        self.duration = self.duration * n
35
36        for effect in self.effects.keys():
37            for i in n:
38                self.effects.update(
39                    effect,
40                    composite_function(self.effects[effect], self.effects[effect]),
41                )
42
43        if n > 0 and n < 1:
44            for effect in self.effects.keys():
45                if effect.target.size * n <= 0.5:
46                    self.effects.update(
47                        effect,
48                        composite_function(
49                            self.effects[effect],
50                            lambda target: math.floor(target.size * n),
51                        ),
52                    )
53                else:
54                    self.effects.update(
55                        effect,
56                        composite_function(
57                            self.effects[effect],
58                            lambda target: math.ceil(target.size * n),
59                        ),
60                    )
61
62    def __sub__(self, other):
63        error = False
64        for key in self.effects.keys():
65            if key not in other.effects.keys():
66                error = True
67
68        if error:
69            raise TypeError("")

.F.EEEEEEEEEEEEEEEEE
======================================================================
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 "/tmp/solution.py", line 26, in __add__
self.effects.update(
TypeError: update expected at most 1 argument, got 2

======================================================================
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 37, in __mul__
for i in n:
TypeError: 'int' object is not iterable

======================================================================
ERROR: test_combination_no_overlap (test.TestPotionOperations)
Test combining potions with no overlap.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 93, in test_combination_no_overlap
potion = potion1 + potion2
File "/tmp/solution.py", line 26, in __add__
self.effects.update(
TypeError: update expected at most 1 argument, got 2

======================================================================
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 26, in __add__
self.effects.update(
TypeError: update expected at most 1 argument, got 2

======================================================================
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 26, in __add__
self.effects.update(
TypeError: update expected at most 1 argument, got 2

======================================================================
ERROR: test_dilution (test.TestPotionOperations)
Test dilution of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 118, in test_dilution
half_potion = base_potion * 0.5
File "/tmp/solution.py", line 37, in __mul__
for i in n:
TypeError: 'float' object is not iterable

======================================================================
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 37, in __mul__
for i in n:
TypeError: 'int' object is not iterable

======================================================================
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 69, in __sub__
raise TypeError("")
TypeError

======================================================================
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 37, in __mul__
for i in n:
TypeError: 'int' object is not iterable

======================================================================
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_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 78, in test_depletion
with self.assertRaisesRegex(TypeError, 'Effect is depleted\.'):
AssertionError: TypeError not raised

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

FAILED (failures=1, errors=17)

Дискусия
Виктор Бечев
03.12.2023 14:04

Не виждам някакви крещящи проблеми, може би само форматирането на `self.effects.update` малко ми бърка в здравето, но това не го прави невъсместимо с PEP8, а лично предпочитание, така че е файн. Then again, това е около 1/3 от домашното, така че ще си оставя поле за изява ако решиш да довършиш. :)
История
Това решение има само една версия.