Домашни > Another brick in the wall > Решения > Решението на Зейнеб Ахмед

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

0 точки общо

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

 1class Material: 
 2    def __init__(self, mass): 
 3        self.mass = mass 
 4
 5    @property 
 6    def volume(self): 
 7        return self.mass / self.density 
 8        
 9class Concrete(Material): 
10    density = 2500 
11
12class Brick(Material): 
13    density = 2000 
14
15class Stone(Material): 
16    density = 1600
17
18class Wood(Material): 
19    density = 600 
20    
21class Steel(Material):
22    density = 7700
23
24
25class Factory:
26    material_classes = {
27        "Concrete": Concrete,
28        "Brick": Brick,
29        "Stone": Stone,
30        "Wood": Wood,
31        "Steel": Steel
32    }
33    existing_classes = {}
34    materials_created = []
35    
36    def __call__(self, *args, **kwargs):
37        if (args and kwargs) or (not args and not kwargs):
38            raise ValueError("Either positional or keyword arguments required, but not both or none.")
39        
40        if args:
41            for material in args:
42                if not isinstance(material, Material):
43                    raise TypeError("All positional arguments must be instances of Material classes.")
44                if getattr(material, 'invalid', False):
45                    raise AssertionError("Cannot use invalid material.")
46            class_name = "_".join(sorted([material.__class__.__name__ for material in args]))
47            if class_name not in Factory.existing_classes:
48                density = sum(material.density for material in args) / len(args)
49                Factory.existing_classes[class_name] = type(class_name, (Material,), {'density': density})
50            new_class = Factory.existing_classes[class_name]
51            new_mass = sum(material.mass for material in args)
52            for material in args:
53                material.invalid = True
54            new_instance = new_class(new_mass)
55            Factory.materials_created.append(new_instance)
56            return new_instance
57
58        materials = []
59        for name, mass in kwargs.items():
60            if name not in self.material_classes:
61                raise ValueError(f"Invalid material name: {name}")
62            material_class = self.material_classes[name]
63            material_instance = material_class(mass)
64            materials.append(material_instance)
65            Factory.materials_created.append(material_instance)
66        return tuple(materials)
67    
68    def can_build_together(required_volume):
69        total_volume = 0
70        for material in Factory.materials_created:
71            if not getattr(material, 'invalid', False):
72                total_volume += material.volume
73        return total_volume >= required_volume
74    
75    @staticmethod
76    def can_build_together(required_volume):
77        total_volume = 0
78        for material in Factory.materials_created:
79            if not getattr(material, 'invalid', False):
80                total_volume += material.volume
81        return total_volume >= required_volume

.EE.E.FF..
======================================================================
ERROR: test_can_build (test.TestFactory.test_can_build)
Test can_build methods.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 173, in test_can_build
self.assertFalse(self.factory1.can_build(2.000001))
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Factory' object has no attribute 'can_build'

======================================================================
ERROR: test_materials_between_factories (test.TestFactory.test_materials_between_factories)
Test materials sharing.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 156, in test_materials_between_factories
result2, = self.factory2(Brick_Concrete_Steel_Stone_Wood=2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 61, in __call__
raise ValueError(f"Invalid material name: {name}")
ValueError: Invalid material name: Brick_Concrete_Steel_Stone_Wood

======================================================================
ERROR: test_named_arguments_with_dynamically_created_classes (test.TestFactory.test_named_arguments_with_dynamically_created_classes)
Test dynamically created classes uniqueness.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 145, in test_named_arguments_with_dynamically_created_classes
result2, = self.factory1(Brick_Concrete_Steel_Stone_Wood=2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 61, in __call__
raise ValueError(f"Invalid material name: {name}")
ValueError: Invalid material name: Brick_Concrete_Steel_Stone_Wood

======================================================================
FAIL: test_positional_arguments_multiple_argument_with_dynamics (test.TestFactory.test_positional_arguments_multiple_argument_with_dynamics)
Test calling a factory using multiple positional arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 128, in test_positional_arguments_multiple_argument_with_dynamics
self.assertEqual(mega_material.__class__.__name__,
AssertionError: 'Brick_Concrete_Stone_Steel_Wood' != 'Brick_Concrete_Steel_Stone_Wood'
- Brick_Concrete_Stone_Steel_Wood
? ------
+ Brick_Concrete_Steel_Stone_Wood
? ++++++

======================================================================
FAIL: test_positional_arguments_single_argument (test.TestFactory.test_positional_arguments_single_argument)
Test calling a factory using a sigle positional argument.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 80, in test_positional_arguments_single_argument
self.assertIs(type(concrete2), solution.Concrete)
AssertionError: <class 'solution.Concrete'> is not <class 'solution.Concrete'>

----------------------------------------------------------------------
Ran 10 tests in 0.011s

FAILED (failures=2, errors=3)

Дискусия
Георги Кунчев
26.11.2024 19:24

Благодаря! Засега губиш само точките от това домашно
Зейнеб Ахмед
26.11.2024 19:04

Така е
Георги Кунчев
26.11.2024 17:45

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