1CONCRETE_DENSITY = 2500
2BRICK_DENSITY = 2000
3STONE_DENSITY = 1600
4WOOD_DENSITY = 600
5STEEL_DENSITY = 7700
6BASE_MATERIALS_COUNT = 5
7BASE_MATERIAL_DENSITIES = [BRICK_DENSITY,
8 CONCRETE_DENSITY,
9 STEEL_DENSITY,
10 STONE_DENSITY,
11 WOOD_DENSITY]
12
13ERROR_MESSAGE = "6ff9dc6e00d1e166f1612e919e934af22eb204b931539f629e152324a2e5a221"
14
15
16class Material:
17 density = 0
18
19 def __init__(self, mass):
20 self.mass = mass
21 self.is_used = False
22
23 @property
24 def volume(self):
25 if self.density == 0:
26 raise ZeroDivisionError(ERROR_MESSAGE)
27 return self.mass / self.density
28
29
30class Concrete(Material):
31
32 def __init__(self, mass):
33 super().__init__(mass)
34 Concrete.density = CONCRETE_DENSITY
35
36
37class Brick(Material):
38
39 def __init__(self, mass):
40 super().__init__(mass)
41 Brick.density = BRICK_DENSITY
42
43
44class Stone(Material):
45
46 def __init__(self, mass):
47 super().__init__(mass)
48 Stone.density = STONE_DENSITY
49
50
51class Wood(Material):
52
53 def __init__(self, mass):
54 super().__init__(mass)
55 Wood.density = WOOD_DENSITY
56
57
58class Steel(Material):
59
60 def __init__(self, mass):
61 super().__init__(mass)
62 Steel.density = STEEL_DENSITY
63
64
65BASE_MATERIAL_CLASSES = [Brick, Concrete, Steel, Stone, Wood]
66BASE_MATERIALS_NAMES = [x.__name__ for x in BASE_MATERIAL_CLASSES]
67
68
69class Factory:
70 alloys = [*BASE_MATERIAL_CLASSES]
71 created_materials_from_factories = []
72
73 def __init__(self):
74 self.created_materials = []
75
76 def __call__(self, *args, **kwargs):
77 if (args and kwargs) or (not args and not kwargs):
78 raise ValueError(ERROR_MESSAGE)
79
80 return self._kwargs_case_handler(**kwargs) if kwargs else self._args_case_handler(*args)
81
82 def _kwargs_case_handler(self, **kwargs):
83 result = []
84 for class_name, mass in kwargs.items():
85 class_type = next((cls for cls in self.alloys if cls.__name__ == class_name), None)
86
87 if class_type is None:
88 raise ValueError(ERROR_MESSAGE)
89
90 current_created_material = class_type(mass)
91 self.created_materials.append(current_created_material)
92 self.created_materials_from_factories.append(current_created_material)
93 result.append(current_created_material)
94
95 return tuple(result)
96
97 def _args_case_handler(self, *args):
98 concatenated_names = "_".join([arg.__class__.__name__ for arg in args])
99 extracted_base_names = set(concatenated_names.split("_"))
100 current_class_name = '_'.join(sorted(extracted_base_names))
101
102 if any([arg.is_used for arg in args]):
103 raise AssertionError(ERROR_MESSAGE)
104
105 if current_class_name not in [current_class.__name__ for current_class in self.alloys]:
106 current_class_density = sum(
107 [BASE_MATERIAL_DENSITIES[i]
108 for i in range(BASE_MATERIALS_COUNT)
109 if BASE_MATERIALS_NAMES[i] in extracted_base_names]
110 )
111 current_class_density /= len(extracted_base_names)
112
113 self.alloys.append(type(current_class_name, (Material,), {"density": current_class_density}))
114
115 for arg in args:
116 arg.is_used = True
117
118 current_instance_mass = sum([arg.mass for arg in args])
119 current_class_type = next(cls for cls in self.alloys if cls.__name__ == current_class_name)
120 current_created_material = current_class_type(current_instance_mass)
121 self.created_materials.append(current_created_material)
122 self.created_materials_from_factories.append(current_created_material)
123 return current_created_material
124
125 def can_build(self, wall_volume):
126 materials_volume = sum(mat.volume for mat in self.created_materials if not mat.is_used)
127 return materials_volume >= wall_volume
128
129 @classmethod
130 def can_build_together(cls, wall_volume):
131 materials_volume = sum(mat.volume for mat in cls.created_materials_from_factories if not mat.is_used)
132 return materials_volume >= wall_volume
..........
----------------------------------------------------------------------
Ran 10 tests in 0.015s
OK
Георги Кунчев
21.11.2024 15:38XOR е супер! Ама ти си решаваш.
Ето как можеш да постигнеш същото наследяване. Аналогично за другите класове.
```
class Concrete(Material):
density = CONCRETE_DENSITY
```
Мисля, че е очевидно, но споменавам - това ти е целият клас. Вече няма да имаш нужда от `__init__`.
|
Даниел Стефанов
21.11.2024 15:35За XOR-a реших да не го променям, защото не ми изглежда четимо. Този коментар не го разбрах: "Ако това го сетнеш като атрибут на класа, както е в Material, ще си спестиш дефиницията на нов __init__ за всички под-класове.". Поне ги оправих да са си клас абтрибути.
|
Георги Кунчев
21.11.2024 13:37Един коментар, който оставям тук, защото не е случай, с който плануваме да тестваме, но си заслужава да се отбележи.
Поради факта, че държиш материали в инстанциите и в класа си, не покриваш случай, който в реалния свят е напълно валиден - дадена фабрика се срутва, съсипвайки всички създадени материали (разбирай `del some_factory`). При този случай не можеш да разбереш кои неща от `created_materials_from_factories` си изгубил и какво да изтриеш.
Ако вместо `created_materials_from_factories` пазиш иснтанциите на `Factory`, ще си ок:
```
class Factory:
_instances = []
def __init__(self):
self.created_materials = []
self._instances.append(self)
```
Мисля, че ти е ясно после как да ги обходиш, ама ако не е, питай.
|
f | 1 | CONCRETE_DENSITY = 2500 | f | 1 | CONCRETE_DENSITY = 2500 |
2 | BRICK_DENSITY = 2000 | 2 | BRICK_DENSITY = 2000 | ||
3 | STONE_DENSITY = 1600 | 3 | STONE_DENSITY = 1600 | ||
4 | WOOD_DENSITY = 600 | 4 | WOOD_DENSITY = 600 | ||
5 | STEEL_DENSITY = 7700 | 5 | STEEL_DENSITY = 7700 | ||
6 | BASE_MATERIALS_COUNT = 5 | 6 | BASE_MATERIALS_COUNT = 5 | ||
7 | BASE_MATERIAL_DENSITIES = [BRICK_DENSITY, | 7 | BASE_MATERIAL_DENSITIES = [BRICK_DENSITY, | ||
8 | CONCRETE_DENSITY, | 8 | CONCRETE_DENSITY, | ||
9 | STEEL_DENSITY, | 9 | STEEL_DENSITY, | ||
10 | STONE_DENSITY, | 10 | STONE_DENSITY, | ||
11 | WOOD_DENSITY] | 11 | WOOD_DENSITY] | ||
12 | 12 | ||||
13 | ERROR_MESSAGE = "6ff9dc6e00d1e166f1612e919e934af22eb204b931539f629e152324a2e5a221" | 13 | ERROR_MESSAGE = "6ff9dc6e00d1e166f1612e919e934af22eb204b931539f629e152324a2e5a221" | ||
14 | 14 | ||||
15 | 15 | ||||
16 | class Material: | 16 | class Material: | ||
17 | density = 0 | 17 | density = 0 | ||
18 | 18 | ||||
19 | def __init__(self, mass): | 19 | def __init__(self, mass): | ||
20 | self.mass = mass | 20 | self.mass = mass | ||
21 | self.is_used = False | 21 | self.is_used = False | ||
22 | 22 | ||||
23 | @property | 23 | @property | ||
24 | def volume(self): | 24 | def volume(self): | ||
25 | if self.density == 0: | 25 | if self.density == 0: | ||
26 | raise ZeroDivisionError(ERROR_MESSAGE) | 26 | raise ZeroDivisionError(ERROR_MESSAGE) | ||
27 | return self.mass / self.density | 27 | return self.mass / self.density | ||
28 | 28 | ||||
29 | 29 | ||||
30 | class Concrete(Material): | 30 | class Concrete(Material): | ||
31 | 31 | ||||
32 | def __init__(self, mass): | 32 | def __init__(self, mass): | ||
33 | super().__init__(mass) | 33 | super().__init__(mass) | ||
n | 34 | self.density = CONCRETE_DENSITY | n | 34 | Concrete.density = CONCRETE_DENSITY |
35 | 35 | ||||
36 | 36 | ||||
37 | class Brick(Material): | 37 | class Brick(Material): | ||
38 | 38 | ||||
39 | def __init__(self, mass): | 39 | def __init__(self, mass): | ||
40 | super().__init__(mass) | 40 | super().__init__(mass) | ||
n | 41 | self.density = BRICK_DENSITY | n | 41 | Brick.density = BRICK_DENSITY |
42 | 42 | ||||
43 | 43 | ||||
44 | class Stone(Material): | 44 | class Stone(Material): | ||
45 | 45 | ||||
46 | def __init__(self, mass): | 46 | def __init__(self, mass): | ||
47 | super().__init__(mass) | 47 | super().__init__(mass) | ||
n | 48 | self.density = STONE_DENSITY | n | 48 | Stone.density = STONE_DENSITY |
49 | 49 | ||||
50 | 50 | ||||
51 | class Wood(Material): | 51 | class Wood(Material): | ||
52 | 52 | ||||
53 | def __init__(self, mass): | 53 | def __init__(self, mass): | ||
54 | super().__init__(mass) | 54 | super().__init__(mass) | ||
n | 55 | self.density = WOOD_DENSITY | n | 55 | Wood.density = WOOD_DENSITY |
56 | 56 | ||||
57 | 57 | ||||
58 | class Steel(Material): | 58 | class Steel(Material): | ||
59 | 59 | ||||
60 | def __init__(self, mass): | 60 | def __init__(self, mass): | ||
61 | super().__init__(mass) | 61 | super().__init__(mass) | ||
t | 62 | self.density = STEEL_DENSITY | t | 62 | Steel.density = STEEL_DENSITY |
63 | 63 | ||||
64 | 64 | ||||
65 | BASE_MATERIAL_CLASSES = [Brick, Concrete, Steel, Stone, Wood] | 65 | BASE_MATERIAL_CLASSES = [Brick, Concrete, Steel, Stone, Wood] | ||
66 | BASE_MATERIALS_NAMES = [x.__name__ for x in BASE_MATERIAL_CLASSES] | 66 | BASE_MATERIALS_NAMES = [x.__name__ for x in BASE_MATERIAL_CLASSES] | ||
67 | 67 | ||||
68 | 68 | ||||
69 | class Factory: | 69 | class Factory: | ||
70 | alloys = [*BASE_MATERIAL_CLASSES] | 70 | alloys = [*BASE_MATERIAL_CLASSES] | ||
71 | created_materials_from_factories = [] | 71 | created_materials_from_factories = [] | ||
72 | 72 | ||||
73 | def __init__(self): | 73 | def __init__(self): | ||
74 | self.created_materials = [] | 74 | self.created_materials = [] | ||
75 | 75 | ||||
76 | def __call__(self, *args, **kwargs): | 76 | def __call__(self, *args, **kwargs): | ||
77 | if (args and kwargs) or (not args and not kwargs): | 77 | if (args and kwargs) or (not args and not kwargs): | ||
78 | raise ValueError(ERROR_MESSAGE) | 78 | raise ValueError(ERROR_MESSAGE) | ||
79 | 79 | ||||
80 | return self._kwargs_case_handler(**kwargs) if kwargs else self._args_case_handler(*args) | 80 | return self._kwargs_case_handler(**kwargs) if kwargs else self._args_case_handler(*args) | ||
81 | 81 | ||||
82 | def _kwargs_case_handler(self, **kwargs): | 82 | def _kwargs_case_handler(self, **kwargs): | ||
83 | result = [] | 83 | result = [] | ||
84 | for class_name, mass in kwargs.items(): | 84 | for class_name, mass in kwargs.items(): | ||
85 | class_type = next((cls for cls in self.alloys if cls.__name__ == class_name), None) | 85 | class_type = next((cls for cls in self.alloys if cls.__name__ == class_name), None) | ||
86 | 86 | ||||
87 | if class_type is None: | 87 | if class_type is None: | ||
88 | raise ValueError(ERROR_MESSAGE) | 88 | raise ValueError(ERROR_MESSAGE) | ||
89 | 89 | ||||
90 | current_created_material = class_type(mass) | 90 | current_created_material = class_type(mass) | ||
91 | self.created_materials.append(current_created_material) | 91 | self.created_materials.append(current_created_material) | ||
92 | self.created_materials_from_factories.append(current_created_material) | 92 | self.created_materials_from_factories.append(current_created_material) | ||
93 | result.append(current_created_material) | 93 | result.append(current_created_material) | ||
94 | 94 | ||||
95 | return tuple(result) | 95 | return tuple(result) | ||
96 | 96 | ||||
97 | def _args_case_handler(self, *args): | 97 | def _args_case_handler(self, *args): | ||
98 | concatenated_names = "_".join([arg.__class__.__name__ for arg in args]) | 98 | concatenated_names = "_".join([arg.__class__.__name__ for arg in args]) | ||
99 | extracted_base_names = set(concatenated_names.split("_")) | 99 | extracted_base_names = set(concatenated_names.split("_")) | ||
100 | current_class_name = '_'.join(sorted(extracted_base_names)) | 100 | current_class_name = '_'.join(sorted(extracted_base_names)) | ||
101 | 101 | ||||
102 | if any([arg.is_used for arg in args]): | 102 | if any([arg.is_used for arg in args]): | ||
103 | raise AssertionError(ERROR_MESSAGE) | 103 | raise AssertionError(ERROR_MESSAGE) | ||
104 | 104 | ||||
105 | if current_class_name not in [current_class.__name__ for current_class in self.alloys]: | 105 | if current_class_name not in [current_class.__name__ for current_class in self.alloys]: | ||
106 | current_class_density = sum( | 106 | current_class_density = sum( | ||
107 | [BASE_MATERIAL_DENSITIES[i] | 107 | [BASE_MATERIAL_DENSITIES[i] | ||
108 | for i in range(BASE_MATERIALS_COUNT) | 108 | for i in range(BASE_MATERIALS_COUNT) | ||
109 | if BASE_MATERIALS_NAMES[i] in extracted_base_names] | 109 | if BASE_MATERIALS_NAMES[i] in extracted_base_names] | ||
110 | ) | 110 | ) | ||
111 | current_class_density /= len(extracted_base_names) | 111 | current_class_density /= len(extracted_base_names) | ||
112 | 112 | ||||
113 | self.alloys.append(type(current_class_name, (Material,), {"density": current_class_density})) | 113 | self.alloys.append(type(current_class_name, (Material,), {"density": current_class_density})) | ||
114 | 114 | ||||
115 | for arg in args: | 115 | for arg in args: | ||
116 | arg.is_used = True | 116 | arg.is_used = True | ||
117 | 117 | ||||
118 | current_instance_mass = sum([arg.mass for arg in args]) | 118 | current_instance_mass = sum([arg.mass for arg in args]) | ||
119 | current_class_type = next(cls for cls in self.alloys if cls.__name__ == current_class_name) | 119 | current_class_type = next(cls for cls in self.alloys if cls.__name__ == current_class_name) | ||
120 | current_created_material = current_class_type(current_instance_mass) | 120 | current_created_material = current_class_type(current_instance_mass) | ||
121 | self.created_materials.append(current_created_material) | 121 | self.created_materials.append(current_created_material) | ||
122 | self.created_materials_from_factories.append(current_created_material) | 122 | self.created_materials_from_factories.append(current_created_material) | ||
123 | return current_created_material | 123 | return current_created_material | ||
124 | 124 | ||||
125 | def can_build(self, wall_volume): | 125 | def can_build(self, wall_volume): | ||
126 | materials_volume = sum(mat.volume for mat in self.created_materials if not mat.is_used) | 126 | materials_volume = sum(mat.volume for mat in self.created_materials if not mat.is_used) | ||
127 | return materials_volume >= wall_volume | 127 | return materials_volume >= wall_volume | ||
128 | 128 | ||||
129 | @classmethod | 129 | @classmethod | ||
130 | def can_build_together(cls, wall_volume): | 130 | def can_build_together(cls, wall_volume): | ||
131 | materials_volume = sum(mat.volume for mat in cls.created_materials_from_factories if not mat.is_used) | 131 | materials_volume = sum(mat.volume for mat in cls.created_materials_from_factories if not mat.is_used) | ||
132 | return materials_volume >= wall_volume | 132 | return materials_volume >= wall_volume |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | CONCRETE_DENSITY = 2500 | f | 1 | CONCRETE_DENSITY = 2500 |
2 | BRICK_DENSITY = 2000 | 2 | BRICK_DENSITY = 2000 | ||
3 | STONE_DENSITY = 1600 | 3 | STONE_DENSITY = 1600 | ||
4 | WOOD_DENSITY = 600 | 4 | WOOD_DENSITY = 600 | ||
5 | STEEL_DENSITY = 7700 | 5 | STEEL_DENSITY = 7700 | ||
n | n | 6 | BASE_MATERIALS_COUNT = 5 | ||
6 | BASE_MATERIAL_DENSITIES = [BRICK_DENSITY, | 7 | BASE_MATERIAL_DENSITIES = [BRICK_DENSITY, | ||
7 | CONCRETE_DENSITY, | 8 | CONCRETE_DENSITY, | ||
8 | STEEL_DENSITY, | 9 | STEEL_DENSITY, | ||
9 | STONE_DENSITY, | 10 | STONE_DENSITY, | ||
10 | WOOD_DENSITY] | 11 | WOOD_DENSITY] | ||
11 | 12 | ||||
12 | ERROR_MESSAGE = "6ff9dc6e00d1e166f1612e919e934af22eb204b931539f629e152324a2e5a221" | 13 | ERROR_MESSAGE = "6ff9dc6e00d1e166f1612e919e934af22eb204b931539f629e152324a2e5a221" | ||
13 | 14 | ||||
14 | 15 | ||||
15 | class Material: | 16 | class Material: | ||
16 | density = 0 | 17 | density = 0 | ||
17 | 18 | ||||
18 | def __init__(self, mass): | 19 | def __init__(self, mass): | ||
19 | self.mass = mass | 20 | self.mass = mass | ||
20 | self.is_used = False | 21 | self.is_used = False | ||
21 | 22 | ||||
22 | @property | 23 | @property | ||
23 | def volume(self): | 24 | def volume(self): | ||
24 | if self.density == 0: | 25 | if self.density == 0: | ||
n | 25 | raise ValueError(ERROR_MESSAGE) | n | 26 | raise ZeroDivisionError(ERROR_MESSAGE) |
26 | return self.mass / self.density | 27 | return self.mass / self.density | ||
27 | 28 | ||||
28 | 29 | ||||
29 | class Concrete(Material): | 30 | class Concrete(Material): | ||
30 | 31 | ||||
31 | def __init__(self, mass): | 32 | def __init__(self, mass): | ||
32 | super().__init__(mass) | 33 | super().__init__(mass) | ||
33 | self.density = CONCRETE_DENSITY | 34 | self.density = CONCRETE_DENSITY | ||
34 | 35 | ||||
35 | 36 | ||||
36 | class Brick(Material): | 37 | class Brick(Material): | ||
37 | 38 | ||||
38 | def __init__(self, mass): | 39 | def __init__(self, mass): | ||
39 | super().__init__(mass) | 40 | super().__init__(mass) | ||
40 | self.density = BRICK_DENSITY | 41 | self.density = BRICK_DENSITY | ||
41 | 42 | ||||
42 | 43 | ||||
43 | class Stone(Material): | 44 | class Stone(Material): | ||
44 | 45 | ||||
45 | def __init__(self, mass): | 46 | def __init__(self, mass): | ||
46 | super().__init__(mass) | 47 | super().__init__(mass) | ||
47 | self.density = STONE_DENSITY | 48 | self.density = STONE_DENSITY | ||
48 | 49 | ||||
49 | 50 | ||||
50 | class Wood(Material): | 51 | class Wood(Material): | ||
51 | 52 | ||||
52 | def __init__(self, mass): | 53 | def __init__(self, mass): | ||
53 | super().__init__(mass) | 54 | super().__init__(mass) | ||
54 | self.density = WOOD_DENSITY | 55 | self.density = WOOD_DENSITY | ||
55 | 56 | ||||
56 | 57 | ||||
57 | class Steel(Material): | 58 | class Steel(Material): | ||
58 | 59 | ||||
59 | def __init__(self, mass): | 60 | def __init__(self, mass): | ||
60 | super().__init__(mass) | 61 | super().__init__(mass) | ||
61 | self.density = STEEL_DENSITY | 62 | self.density = STEEL_DENSITY | ||
62 | 63 | ||||
63 | 64 | ||||
n | 64 | base_materials_classes = [Brick, Concrete, Steel, Stone, Wood] | n | 65 | BASE_MATERIAL_CLASSES = [Brick, Concrete, Steel, Stone, Wood] |
65 | base_materials_names = [x.__name__ for x in base_materials_classes] | 66 | BASE_MATERIALS_NAMES = [x.__name__ for x in BASE_MATERIAL_CLASSES] | ||
66 | 67 | ||||
67 | 68 | ||||
68 | class Factory: | 69 | class Factory: | ||
n | 69 | alloys = [*base_materials_classes] | n | 70 | alloys = [*BASE_MATERIAL_CLASSES] |
70 | created_materials_from_factories = [] | 71 | created_materials_from_factories = [] | ||
71 | 72 | ||||
72 | def __init__(self): | 73 | def __init__(self): | ||
73 | self.created_materials = [] | 74 | self.created_materials = [] | ||
74 | 75 | ||||
75 | def __call__(self, *args, **kwargs): | 76 | def __call__(self, *args, **kwargs): | ||
76 | if (args and kwargs) or (not args and not kwargs): | 77 | if (args and kwargs) or (not args and not kwargs): | ||
77 | raise ValueError(ERROR_MESSAGE) | 78 | raise ValueError(ERROR_MESSAGE) | ||
78 | 79 | ||||
79 | return self._kwargs_case_handler(**kwargs) if kwargs else self._args_case_handler(*args) | 80 | return self._kwargs_case_handler(**kwargs) if kwargs else self._args_case_handler(*args) | ||
80 | 81 | ||||
81 | def _kwargs_case_handler(self, **kwargs): | 82 | def _kwargs_case_handler(self, **kwargs): | ||
n | 82 | res = [] | n | 83 | result = [] |
83 | for class_name, mass in kwargs.items(): | 84 | for class_name, mass in kwargs.items(): | ||
84 | class_type = next((cls for cls in self.alloys if cls.__name__ == class_name), None) | 85 | class_type = next((cls for cls in self.alloys if cls.__name__ == class_name), None) | ||
85 | 86 | ||||
86 | if class_type is None: | 87 | if class_type is None: | ||
87 | raise ValueError(ERROR_MESSAGE) | 88 | raise ValueError(ERROR_MESSAGE) | ||
88 | 89 | ||||
89 | current_created_material = class_type(mass) | 90 | current_created_material = class_type(mass) | ||
90 | self.created_materials.append(current_created_material) | 91 | self.created_materials.append(current_created_material) | ||
91 | self.created_materials_from_factories.append(current_created_material) | 92 | self.created_materials_from_factories.append(current_created_material) | ||
n | 92 | res.append(current_created_material) | n | 93 | result.append(current_created_material) |
93 | 94 | ||||
n | 94 | return tuple(res) | n | 95 | return tuple(result) |
95 | 96 | ||||
96 | def _args_case_handler(self, *args): | 97 | def _args_case_handler(self, *args): | ||
97 | concatenated_names = "_".join([arg.__class__.__name__ for arg in args]) | 98 | concatenated_names = "_".join([arg.__class__.__name__ for arg in args]) | ||
98 | extracted_base_names = set(concatenated_names.split("_")) | 99 | extracted_base_names = set(concatenated_names.split("_")) | ||
99 | current_class_name = '_'.join(sorted(extracted_base_names)) | 100 | current_class_name = '_'.join(sorted(extracted_base_names)) | ||
100 | 101 | ||||
101 | if any([arg.is_used for arg in args]): | 102 | if any([arg.is_used for arg in args]): | ||
102 | raise AssertionError(ERROR_MESSAGE) | 103 | raise AssertionError(ERROR_MESSAGE) | ||
103 | 104 | ||||
104 | if current_class_name not in [current_class.__name__ for current_class in self.alloys]: | 105 | if current_class_name not in [current_class.__name__ for current_class in self.alloys]: | ||
105 | current_class_density = sum( | 106 | current_class_density = sum( | ||
106 | [BASE_MATERIAL_DENSITIES[i] | 107 | [BASE_MATERIAL_DENSITIES[i] | ||
t | 107 | for i in range(0, 5) | t | 108 | for i in range(BASE_MATERIALS_COUNT) |
108 | if base_materials_names[i] in extracted_base_names] | 109 | if BASE_MATERIALS_NAMES[i] in extracted_base_names] | ||
109 | ) | 110 | ) | ||
110 | current_class_density /= len(extracted_base_names) | 111 | current_class_density /= len(extracted_base_names) | ||
111 | 112 | ||||
112 | self.alloys.append(type(current_class_name, (Material,), {"density": current_class_density})) | 113 | self.alloys.append(type(current_class_name, (Material,), {"density": current_class_density})) | ||
113 | 114 | ||||
114 | for arg in args: | 115 | for arg in args: | ||
115 | arg.is_used = True | 116 | arg.is_used = True | ||
116 | 117 | ||||
117 | current_instance_mass = sum([arg.mass for arg in args]) | 118 | current_instance_mass = sum([arg.mass for arg in args]) | ||
118 | current_class_type = next(cls for cls in self.alloys if cls.__name__ == current_class_name) | 119 | current_class_type = next(cls for cls in self.alloys if cls.__name__ == current_class_name) | ||
119 | current_created_material = current_class_type(current_instance_mass) | 120 | current_created_material = current_class_type(current_instance_mass) | ||
120 | self.created_materials.append(current_created_material) | 121 | self.created_materials.append(current_created_material) | ||
121 | self.created_materials_from_factories.append(current_created_material) | 122 | self.created_materials_from_factories.append(current_created_material) | ||
122 | return current_created_material | 123 | return current_created_material | ||
123 | 124 | ||||
124 | def can_build(self, wall_volume): | 125 | def can_build(self, wall_volume): | ||
125 | materials_volume = sum(mat.volume for mat in self.created_materials if not mat.is_used) | 126 | materials_volume = sum(mat.volume for mat in self.created_materials if not mat.is_used) | ||
126 | return materials_volume >= wall_volume | 127 | return materials_volume >= wall_volume | ||
127 | 128 | ||||
128 | @classmethod | 129 | @classmethod | ||
129 | def can_build_together(cls, wall_volume): | 130 | def can_build_together(cls, wall_volume): | ||
130 | materials_volume = sum(mat.volume for mat in cls.created_materials_from_factories if not mat.is_used) | 131 | materials_volume = sum(mat.volume for mat in cls.created_materials_from_factories if not mat.is_used) | ||
131 | return materials_volume >= wall_volume | 132 | return materials_volume >= wall_volume |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|