Домашни > Another brick in the wall > Решения > Решението на Ипек Ибрахим

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

11 точки общо

10 успешни теста
0 неуспешни теста
Код

 1class Material:
 2
 3    density = -1
 4
 5    def __init__(self, weight):
 6        self.weight = weight
 7        self.used = False
 8
 9    @property
10    def volume(self):
11        return self.weight / self.density
12
13
14class Concrete(Material):
15
16    density = 2500
17
18
19class Brick(Material):
20
21    density = 2000
22
23
24class Stone(Material):
25
26    density = 1600
27
28
29class Wood(Material):
30
31    density = 600
32
33
34class Steel(Material):
35
36    density = 7700
37
38
39existing_materials = {
40    "Concrete" : Concrete, "Brick" : Brick,
41    "Stone" : Stone, "Wood" : Wood, "Steel" : Steel
42    }
43
44
45class Factory:
46
47    _all_factories = []
48
49    def __init__(self):
50        self.materials_made = []
51        Factory._all_factories.append(self)
52
53    def _create_alloy(self, *args):
54        unique_material_names = []
55        for material in args:
56            unique_material_names.extend(material.__class__.__name__.split("_"))
57        alloy_name = "_".join(sorted(unique_material_names))
58        try:
59            for material in args:
60                material.used = True
61            return existing_materials[alloy_name](sum(material.weight for material in args))
62        except KeyError:
63            density = sum(existing_materials[material_name].density 
64                          for material_name in unique_material_names) / (alloy_name.count("_") + 1)
65            existing_materials[alloy_name] = type(alloy_name, (Material, ), {'density': density})
66            return existing_materials[alloy_name](sum(material.weight for material in args)) 
67         
68    def __call__(self, *args, **kwargs):
69        if not args and not kwargs:
70            raise ValueError("Cannot call Factory without arguments.")
71        if args and kwargs:
72            raise ValueError("Cannot call Factory with both positional and keyword arguments.")  
73        if kwargs:
74            try:
75                materials = []
76                for material, weight in kwargs.items():
77                    materials.append(existing_materials[material](weight))
78            except KeyError:
79                raise ValueError("Incorrect input for materials.")
80            self.materials_made.extend(materials)
81            return tuple(materials)
82        else:
83            if any(material.used for material in args):
84                raise AssertionError("Materials already used.")
85            else:
86                new_alloy = self._create_alloy(*args)
87                self.materials_made.append(new_alloy)
88                return new_alloy
89
90    def can_build(self, target_volume):
91        return sum(material.volume for material in self.materials_made if not material.used) >= target_volume
92
93    @classmethod
94    def can_build_together(cls, target_volume):
95        total_volume = 0
96        for factory in cls._all_factories:
97            total_volume += sum(material.volume for material in factory.materials_made if not material.used)
98        return total_volume >= target_volume

..........
----------------------------------------------------------------------
Ran 10 tests in 0.009s

OK

Дискусия
Виктор Бечев
24.11.2024 22:21

Отвъд дребните забележки - доста чисто решение.
История

f1class Material:f1class Material:
nn2 
3    density = -1
24
3    def __init__(self, weight):5    def __init__(self, weight):
4        self.weight = weight6        self.weight = weight
5        self.used = False7        self.used = False
n6    n
7    density = -1
88
9    @property9    @property
10    def volume(self):10    def volume(self):
11        return self.weight / self.density11        return self.weight / self.density
1212
1313
14class Concrete(Material):14class Concrete(Material):
1515
16    density = 250016    density = 2500
1717
1818
19class Brick(Material):19class Brick(Material):
2020
21    density = 200021    density = 2000
2222
2323
24class Stone(Material):24class Stone(Material):
2525
26    density = 160026    density = 1600
2727
2828
29class Wood(Material):29class Wood(Material):
3030
31    density = 60031    density = 600
3232
3333
34class Steel(Material):34class Steel(Material):
3535
36    density = 770036    density = 7700
3737
3838
39existing_materials = {39existing_materials = {
40    "Concrete" : Concrete, "Brick" : Brick,40    "Concrete" : Concrete, "Brick" : Brick,
41    "Stone" : Stone, "Wood" : Wood, "Steel" : Steel41    "Stone" : Stone, "Wood" : Wood, "Steel" : Steel
42    }42    }
4343
4444
45class Factory:45class Factory:
4646
n47    all_factories = []n47    _all_factories = []
4848
49    def __init__(self):49    def __init__(self):
50        self.materials_made = []50        self.materials_made = []
n51        Factory.all_factories.append(self)n51        Factory._all_factories.append(self)
5252
53    def _create_alloy(self, *args):53    def _create_alloy(self, *args):
54        unique_material_names = []54        unique_material_names = []
55        for material in args:55        for material in args:
56            unique_material_names.extend(material.__class__.__name__.split("_"))56            unique_material_names.extend(material.__class__.__name__.split("_"))
57        alloy_name = "_".join(sorted(unique_material_names))57        alloy_name = "_".join(sorted(unique_material_names))
58        try:58        try:
n59            existing_materials[alloy_name]n59            for material in args:
60                material.used = True
61            return existing_materials[alloy_name](sum(material.weight for material in args))
60        except KeyError:62        except KeyError:
nn63            density = sum(existing_materials[material_name].density 
64                          for material_name in unique_material_names) / (alloy_name.count("_") + 1)
61            existing_materials[alloy_name] = type(alloy_name,65            existing_materials[alloy_name] = type(alloy_name, (Material, ), {'density': density})
62                                                (Material, ),
63                                                {
64                                                    'density': sum(
65                                                        existing_materials[material_name].density
66                                                        for material_name in unique_material_names
67                                                        )
68                                                        / (alloy_name.count("_") + 1)
69                                                }
70                                            )
71        finally:
72            for material in args:
73                material.used = True 
74            return existing_materials[alloy_name](sum(material.weight for material in args))66            return existing_materials[alloy_name](sum(material.weight for material in args)) 
75 67         
76    def __call__(self, *args, **kwargs):68    def __call__(self, *args, **kwargs):
77        if not args and not kwargs:69        if not args and not kwargs:
78            raise ValueError("Cannot call Factory without arguments.")70            raise ValueError("Cannot call Factory without arguments.")
79        if args and kwargs:71        if args and kwargs:
80            raise ValueError("Cannot call Factory with both positional and keyword arguments.")  72            raise ValueError("Cannot call Factory with both positional and keyword arguments.")  
81        if kwargs:73        if kwargs:
82            try:74            try:
83                materials = []75                materials = []
84                for material, weight in kwargs.items():76                for material, weight in kwargs.items():
85                    materials.append(existing_materials[material](weight))77                    materials.append(existing_materials[material](weight))
86            except KeyError:78            except KeyError:
87                raise ValueError("Incorrect input for materials.")79                raise ValueError("Incorrect input for materials.")
88            self.materials_made.extend(materials)80            self.materials_made.extend(materials)
89            return tuple(materials)81            return tuple(materials)
90        else:82        else:
91            if any(material.used for material in args):83            if any(material.used for material in args):
92                raise AssertionError("Materials already used.")84                raise AssertionError("Materials already used.")
93            else:85            else:
94                new_alloy = self._create_alloy(*args)86                new_alloy = self._create_alloy(*args)
95                self.materials_made.append(new_alloy)87                self.materials_made.append(new_alloy)
96                return new_alloy88                return new_alloy
9789
98    def can_build(self, target_volume):90    def can_build(self, target_volume):
99        return sum(material.volume for material in self.materials_made if not material.used) >= target_volume91        return sum(material.volume for material in self.materials_made if not material.used) >= target_volume
10092
101    @classmethod93    @classmethod
102    def can_build_together(cls, target_volume):94    def can_build_together(cls, target_volume):
103        total_volume = 095        total_volume = 0
t104        for factory in cls.all_factories:t96        for factory in cls._all_factories:
105            total_volume += sum(material.volume for material in factory.materials_made if not material.used)97            total_volume += sum(material.volume for material in factory.materials_made if not material.used)
106        return total_volume >= target_volume98        return total_volume >= target_volume
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op