1class Material:
2 def __init__(self, value, density):
3 self.value = value
4 self.density = density
5
6 @property
7 def volume(self):
8 return self.value/self.density
9
10
11class Concrete(Material):
12 def __init__(self, value):
13 super().__init__(value, density=2500)
14
15
16class Brick(Material):
17 def __init__(self, value):
18 super().__init__(value, density=2000)
19
20
21class Stone(Material):
22 def __init__(self, value):
23 super().__init__(value, density=1600)
24
25
26class Wood(Material):
27 def __init__(self, value):
28 super().__init__(value, density=600)
29
30
31class Steel(Material):
32 def __init__(self, value):
33 super().__init__(value, density=7700)
34
35
36class Factory:
37 def __init__(self):
38 self.materials = []
39
40 def __call__(self, *args, **kwargs):
41 if not args and not kwargs or args and kwargs:
42 raise ValueError('Invalid operation')
43 if args:
44 new_name = '_'.join(sorted(type(arg).__name__ for arg in args))
45 if new_name not in self.materials:
46 new_value = sum(arg.value for arg in args)
47 new_density = sum(arg.density for arg in args)/len(args)
48 new_class = type(new_name, (Material, ), {})
49 new_material = new_class(new_value, new_density)
50 self.materials.append(new_material)
51 return new_material
52 if kwargs:
53 for name, value in kwargs.items():
54 materials = [subcls for subcls in Material.__subclasses__() if subcls.__name__ == name]
55 if materials:
56 self.materials.append(materials[0](value))
57 else:
58 raise ValueError('The material is not in the list')
59 return tuple(self.materials)
60
61 def can_build(self, volume):
62 total_volume = sum(material.volume for material in self.materials)
63 return total_volume >= volume
.FEFEEEFF.
======================================================================
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 56, in __call__
self.materials.append(materials[0](value))
^^^^^^^^^^^^^^^^^^^
TypeError: Material.__init__() missing 1 required positional argument: 'density'
======================================================================
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 56, in __call__
self.materials.append(materials[0](value))
^^^^^^^^^^^^^^^^^^^
TypeError: Material.__init__() missing 1 required positional argument: 'density'
======================================================================
ERROR: test_positional_arguments_multiple_argument_from_initial_set (test.TestFactory.test_positional_arguments_multiple_argument_from_initial_set)
Test calling a factory using multiple positional arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 97, in test_positional_arguments_multiple_argument_from_initial_set
brick_concrete_wood = type(result)(1700)
^^^^^^^^^^^^^^^^^^
TypeError: Material.__init__() missing 1 required positional argument: 'density'
======================================================================
ERROR: 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 126, in test_positional_arguments_multiple_argument_with_dynamics
steel, wood = self.factory1(Steel=7700, Wood=1200)
^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
======================================================================
FAIL: test_can_build (test.TestFactory.test_can_build)
Test can_build methods.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 175, in test_can_build
self.assertFalse(self.factory2.can_build(4.0001))
AssertionError: True is not false
======================================================================
FAIL: test_named_arguments (test.TestFactory.test_named_arguments)
Test calling a factory using named arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 66, in test_named_arguments
self.assertIs(type(result[1]), solution.Brick)
AssertionError: <class 'solution.Concrete'> is not <class 'solution.Brick'>
======================================================================
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'>
======================================================================
FAIL: test_positional_arguments_singletons (test.TestFactory.test_positional_arguments_singletons)
Test dynamically created classes uniqueness.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 138, in test_positional_arguments_singletons
self.assertIs(type(brick_concrete_wood1), type(brick_concrete_wood2))
AssertionError: <class 'solution.Brick_Concrete_Wood'> is not <class 'solution.Brick_Brick_Brick_Concrete_Wood_Concrete_Concrete_Wood_Wood'>
----------------------------------------------------------------------
Ran 10 tests in 0.009s
FAILED (failures=4, errors=4)
Георги Кунчев
26.11.2024 14:28Много стегнато решение, което има потенциала да се превърне в добър продукт, ако се завърши :slightly_smiling_face:
|
26.11.2024 14:27
26.11.2024 14:26