Домашни > Another brick in the wall > Решения > Решението на Жан Георгиев

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

6 точки общо

6 успешни теста
4 неуспешни теста
Код

  1class Material:
  2    def __init__(self, mass):
  3        self.mass = mass
  4        self.used = False
  5
  6    @property
  7    def volume(self):
  8        return self.mass / self.density
  9
 10
 11class Concrete(Material):
 12    density = 2500
 13
 14
 15class Brick(Material):
 16    density = 2000
 17
 18
 19class Stone(Material):
 20    density = 1600
 21
 22
 23class Wood(Material):
 24    density = 600
 25
 26
 27class Steel(Material):
 28    density = 7700
 29
 30
 31class Factory:
 32    all_factories = []
 33
 34    known_materials = {
 35        'Concrete': Concrete,
 36        'Brick': Brick,
 37        'Stone': Stone,
 38        'Wood': Wood,
 39        'Steel': Steel
 40        }
 41
 42    def __init__(self):
 43        self.created = []
 44        Factory.all_factories.append(self)
 45
 46    def __call__(self, *args, **kargs):
 47        """This method is not long, it's monumental."""
 48        if args and kargs or not args and not kargs:
 49            raise ValueError
 50
 51        if kargs:
 52            result = []
 53            for material_name, mass in kargs.items():
 54                if material_name not in Factory.known_materials:
 55                    raise ValueError(f"Unknown material: {material_name}")
 56
 57                material = Factory.known_materials[material_name]
 58                self.created.append(Factory.known_materials[material_name](mass))
 59                result.append(material(mass))
 60
 61            return tuple(result)
 62
 63        if args:
 64            for material in args:
 65                if material.__class__.__name__ not in Factory.known_materials:
 66                    raise ValueError(f"Unknown material: {material_name}")
 67
 68            for material in args:
 69                if material.used:
 70                    raise AssertionError
 71
 72            for material in args:
 73                material.used = True
 74
 75            for material in self.created:
 76                for names in args:
 77                    if material.__class__.__name__ is names.__class__.__name__:
 78                        material.used = True
 79
 80            material_name = []
 81
 82            for material in args:
 83                material_name.append(material.__class__.__name__)
 84
 85            new_material_name = '_'.join(material_name)
 86
 87            material_mass = sum(material.mass for material in args)
 88
 89            if new_material_name in Factory.known_materials:
 90                self.created.append(Factory.known_materials[new_material_name](material_mass))
 91                return Factory.known_materials[new_material_name](material_mass)
 92
 93            original_materials_names = new_material_name.split('_')
 94
 95            material_density = 0
 96            for material in original_materials_names:
 97                material_density += Factory.known_materials[material].density
 98
 99            material_density /= len(original_materials_names)
100
101            new_material = type(new_material_name, (Material,), {
102                'density': material_density,
103            })
104
105            Factory.known_materials[new_material_name] = new_material
106            self.created.append(Factory.known_materials[new_material_name](material_mass))
107
108            return new_material(material_mass)
109
110    def get_total_volume(self):
111        total_volume = 0
112        for materials in self.created:
113            if materials.used is False:
114                total_volume += materials.volume
115        return total_volume
116
117    def can_build(self, volume_needed):
118        return self.get_total_volume() >= volume_needed
119
120    @classmethod
121    def can_build_together(cls, volume_needed):
122        total_volume = 0
123
124        for factories in cls.all_factories:
125            total_volume += factories.get_total_volume()
126        return total_volume >= volume_needed

..E.EFF...
======================================================================
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 55, in __call__
raise ValueError(f"Unknown material: {material_name}")
ValueError: Unknown material: 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 55, in __call__
raise ValueError(f"Unknown material: {material_name}")
ValueError: Unknown material: Brick_Concrete_Steel_Stone_Wood

======================================================================
FAIL: 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 94, in test_positional_arguments_multiple_argument_from_initial_set
self.assertEqual(result.__class__.__name__, 'Brick_Concrete_Wood')
AssertionError: 'Wood_Brick_Concrete' != 'Brick_Concrete_Wood'
- Wood_Brick_Concrete
? -----
+ Brick_Concrete_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: 'Steel_Stone_Concrete_Brick_Wood' != 'Brick_Concrete_Steel_Stone_Wood'
- Steel_Stone_Concrete_Brick_Wood
+ Brick_Concrete_Steel_Stone_Wood

----------------------------------------------------------------------
Ran 10 tests in 0.010s

FAILED (failures=2, errors=2)

Дискусия
История

f1class Material:f1class Material:
2    def __init__(self, mass):2    def __init__(self, mass):
3        self.mass = mass3        self.mass = mass
4        self.used = False4        self.used = False
55
6    @property6    @property
7    def volume(self):7    def volume(self):
8        return self.mass / self.density8        return self.mass / self.density
99
1010
11class Concrete(Material):11class Concrete(Material):
12    density = 250012    density = 2500
1313
1414
15class Brick(Material):15class Brick(Material):
16    density = 200016    density = 2000
1717
1818
19class Stone(Material):19class Stone(Material):
20    density = 160020    density = 1600
2121
2222
23class Wood(Material):23class Wood(Material):
24    density = 60024    density = 600
2525
2626
27class Steel(Material):27class Steel(Material):
28    density = 770028    density = 7700
2929
3030
31class Factory:31class Factory:
32    all_factories = []32    all_factories = []
3333
34    known_materials = {34    known_materials = {
35        'Concrete': Concrete,35        'Concrete': Concrete,
36        'Brick': Brick,36        'Brick': Brick,
37        'Stone': Stone,37        'Stone': Stone,
38        'Wood': Wood,38        'Wood': Wood,
39        'Steel': Steel39        'Steel': Steel
40        }40        }
4141
42    def __init__(self):42    def __init__(self):
43        self.created = []43        self.created = []
44        Factory.all_factories.append(self)44        Factory.all_factories.append(self)
4545
46    def __call__(self, *args, **kargs):46    def __call__(self, *args, **kargs):
47        """This method is not long, it's monumental."""47        """This method is not long, it's monumental."""
48        if args and kargs or not args and not kargs:48        if args and kargs or not args and not kargs:
49            raise ValueError49            raise ValueError
5050
51        if kargs:51        if kargs:
52            result = []52            result = []
53            for material_name, mass in kargs.items():53            for material_name, mass in kargs.items():
54                if material_name not in Factory.known_materials:54                if material_name not in Factory.known_materials:
55                    raise ValueError(f"Unknown material: {material_name}")55                    raise ValueError(f"Unknown material: {material_name}")
5656
57                material = Factory.known_materials[material_name]57                material = Factory.known_materials[material_name]
58                self.created.append(Factory.known_materials[material_name](mass))58                self.created.append(Factory.known_materials[material_name](mass))
59                result.append(material(mass))59                result.append(material(mass))
6060
61            return tuple(result)61            return tuple(result)
6262
63        if args:63        if args:
64            for material in args:64            for material in args:
65                if material.__class__.__name__ not in Factory.known_materials:65                if material.__class__.__name__ not in Factory.known_materials:
66                    raise ValueError(f"Unknown material: {material_name}")66                    raise ValueError(f"Unknown material: {material_name}")
6767
68            for material in args:68            for material in args:
69                if material.used:69                if material.used:
70                    raise AssertionError70                    raise AssertionError
7171
72            for material in args:72            for material in args:
73                material.used = True73                material.used = True
7474
75            for material in self.created:75            for material in self.created:
76                for names in args:76                for names in args:
77                    if material.__class__.__name__ is names.__class__.__name__:77                    if material.__class__.__name__ is names.__class__.__name__:
78                        material.used = True78                        material.used = True
7979
80            material_name = []80            material_name = []
8181
82            for material in args:82            for material in args:
83                material_name.append(material.__class__.__name__)83                material_name.append(material.__class__.__name__)
8484
85            new_material_name = '_'.join(material_name)85            new_material_name = '_'.join(material_name)
8686
87            material_mass = sum(material.mass for material in args)87            material_mass = sum(material.mass for material in args)
8888
89            if new_material_name in Factory.known_materials:89            if new_material_name in Factory.known_materials:
90                self.created.append(Factory.known_materials[new_material_name](material_mass))90                self.created.append(Factory.known_materials[new_material_name](material_mass))
91                return Factory.known_materials[new_material_name](material_mass)91                return Factory.known_materials[new_material_name](material_mass)
9292
93            original_materials_names = new_material_name.split('_')93            original_materials_names = new_material_name.split('_')
9494
95            material_density = 095            material_density = 0
96            for material in original_materials_names:96            for material in original_materials_names:
97                material_density += Factory.known_materials[material].density97                material_density += Factory.known_materials[material].density
9898
99            material_density /= len(original_materials_names)99            material_density /= len(original_materials_names)
100100
101            new_material = type(new_material_name, (Material,), {101            new_material = type(new_material_name, (Material,), {
102                'density': material_density,102                'density': material_density,
103            })103            })
104104
105            Factory.known_materials[new_material_name] = new_material105            Factory.known_materials[new_material_name] = new_material
106            self.created.append(Factory.known_materials[new_material_name](material_mass))106            self.created.append(Factory.known_materials[new_material_name](material_mass))
107107
108            return new_material(material_mass)108            return new_material(material_mass)
109109
110    def get_total_volume(self):110    def get_total_volume(self):
111        total_volume = 0111        total_volume = 0
112        for materials in self.created:112        for materials in self.created:
113            if materials.used is False:113            if materials.used is False:
114                total_volume += materials.volume114                total_volume += materials.volume
115        return total_volume115        return total_volume
116116
117    def can_build(self, volume_needed):117    def can_build(self, volume_needed):
118        return self.get_total_volume() >= volume_needed118        return self.get_total_volume() >= volume_needed
119119
120    @classmethod120    @classmethod
121    def can_build_together(cls, volume_needed):121    def can_build_together(cls, volume_needed):
122        total_volume = 0122        total_volume = 0
123123
t124        for cur in range(0, len(cls.all_factories)):t124        for factories in cls.all_factories:
125            total_volume += cls.all_factories[cur].get_total_volume()125            total_volume += factories.get_total_volume()
126        return total_volume >= volume_needed126        return total_volume >= volume_needed
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Material:f1class Material:
2    def __init__(self, mass):2    def __init__(self, mass):
3        self.mass = mass3        self.mass = mass
4        self.used = False4        self.used = False
55
6    @property6    @property
7    def volume(self):7    def volume(self):
8        return self.mass / self.density8        return self.mass / self.density
99
1010
11class Concrete(Material):11class Concrete(Material):
12    density = 250012    density = 2500
1313
1414
15class Brick(Material):15class Brick(Material):
16    density = 200016    density = 2000
1717
1818
19class Stone(Material):19class Stone(Material):
20    density = 160020    density = 1600
2121
2222
23class Wood(Material):23class Wood(Material):
24    density = 60024    density = 600
2525
2626
27class Steel(Material):27class Steel(Material):
28    density = 770028    density = 7700
2929
3030
31class Factory:31class Factory:
32    all_factories = []32    all_factories = []
3333
34    known_materials = {34    known_materials = {
35        'Concrete': Concrete,35        'Concrete': Concrete,
36        'Brick': Brick,36        'Brick': Brick,
37        'Stone': Stone,37        'Stone': Stone,
38        'Wood': Wood,38        'Wood': Wood,
39        'Steel': Steel39        'Steel': Steel
40        }40        }
4141
42    def __init__(self):42    def __init__(self):
43        self.created = []43        self.created = []
44        Factory.all_factories.append(self)44        Factory.all_factories.append(self)
4545
46    def __call__(self, *args, **kargs):46    def __call__(self, *args, **kargs):
nn47        """This method is not long, it's monumental."""
47        if args and kargs or not args and not kargs:48        if args and kargs or not args and not kargs:
48            raise ValueError49            raise ValueError
4950
50        if kargs:51        if kargs:
51            result = []52            result = []
52            for material_name, mass in kargs.items():53            for material_name, mass in kargs.items():
53                if material_name not in Factory.known_materials:54                if material_name not in Factory.known_materials:
54                    raise ValueError(f"Unknown material: {material_name}")55                    raise ValueError(f"Unknown material: {material_name}")
5556
56                material = Factory.known_materials[material_name]57                material = Factory.known_materials[material_name]
57                self.created.append(Factory.known_materials[material_name](mass))58                self.created.append(Factory.known_materials[material_name](mass))
58                result.append(material(mass))59                result.append(material(mass))
5960
60            return tuple(result)61            return tuple(result)
6162
62        if args:63        if args:
63            for material in args:64            for material in args:
64                if material.__class__.__name__ not in Factory.known_materials:65                if material.__class__.__name__ not in Factory.known_materials:
65                    raise ValueError(f"Unknown material: {material_name}")66                    raise ValueError(f"Unknown material: {material_name}")
6667
67            for material in args:68            for material in args:
68                if material.used:69                if material.used:
69                    raise AssertionError70                    raise AssertionError
7071
71            for material in args:72            for material in args:
72                material.used = True73                material.used = True
7374
74            for material in self.created:75            for material in self.created:
75                for names in args:76                for names in args:
76                    if material.__class__.__name__ is names.__class__.__name__:77                    if material.__class__.__name__ is names.__class__.__name__:
77                        material.used = True78                        material.used = True
7879
79            material_name = []80            material_name = []
8081
81            for material in args:82            for material in args:
82                material_name.append(material.__class__.__name__)83                material_name.append(material.__class__.__name__)
8384
84            new_material_name = '_'.join(material_name)85            new_material_name = '_'.join(material_name)
8586
86            material_mass = sum(material.mass for material in args)87            material_mass = sum(material.mass for material in args)
8788
88            if new_material_name in Factory.known_materials:89            if new_material_name in Factory.known_materials:
89                self.created.append(Factory.known_materials[new_material_name](material_mass))90                self.created.append(Factory.known_materials[new_material_name](material_mass))
90                return Factory.known_materials[new_material_name](material_mass)91                return Factory.known_materials[new_material_name](material_mass)
9192
92            original_materials_names = new_material_name.split('_')93            original_materials_names = new_material_name.split('_')
9394
94            material_density = 095            material_density = 0
95            for material in original_materials_names:96            for material in original_materials_names:
96                material_density += Factory.known_materials[material].density97                material_density += Factory.known_materials[material].density
9798
98            material_density /= len(original_materials_names)99            material_density /= len(original_materials_names)
99100
100            new_material = type(new_material_name, (Material,), {101            new_material = type(new_material_name, (Material,), {
101                'density': material_density,102                'density': material_density,
t102                '__init__': lambda self, mass: Material.__init__(self, mass),t
103                'volume': property(lambda self: self.mass / self.density),
104                'used': False,
105            })103            })
106104
107            Factory.known_materials[new_material_name] = new_material105            Factory.known_materials[new_material_name] = new_material
108            self.created.append(Factory.known_materials[new_material_name](material_mass))106            self.created.append(Factory.known_materials[new_material_name](material_mass))
109107
110            return new_material(material_mass)108            return new_material(material_mass)
111109
112    def get_total_volume(self):110    def get_total_volume(self):
113        total_volume = 0111        total_volume = 0
114        for materials in self.created:112        for materials in self.created:
115            if materials.used is False:113            if materials.used is False:
116                total_volume += materials.volume114                total_volume += materials.volume
117        return total_volume115        return total_volume
118116
119    def can_build(self, volume_needed):117    def can_build(self, volume_needed):
120        return self.get_total_volume() >= volume_needed118        return self.get_total_volume() >= volume_needed
121119
122    @classmethod120    @classmethod
123    def can_build_together(cls, volume_needed):121    def can_build_together(cls, volume_needed):
124        total_volume = 0122        total_volume = 0
125123
126        for cur in range(0, len(cls.all_factories)):124        for cur in range(0, len(cls.all_factories)):
127            total_volume += cls.all_factories[cur].get_total_volume()125            total_volume += cls.all_factories[cur].get_total_volume()
128        return total_volume >= volume_needed126        return total_volume >= volume_needed
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class Material:f1class Material:
2    def __init__(self, mass):2    def __init__(self, mass):
3        self.mass = mass3        self.mass = mass
4        self.used = False4        self.used = False
55
6    @property6    @property
7    def volume(self):7    def volume(self):
8        return self.mass / self.density8        return self.mass / self.density
99
nn10 
10class Concrete(Material):11class Concrete(Material):
n11    density = 2500  n12    density = 2500
13 
1214
13class Brick(Material):15class Brick(Material):
n14    density = 2000  n16    density = 2000
17 
1518
16class Stone(Material):19class Stone(Material):
n17    density = 1600  n20    density = 1600
21 
1822
19class Wood(Material):23class Wood(Material):
20    density = 60024    density = 600
2125
nn26 
22class Steel(Material):27class Steel(Material):
23    density = 770028    density = 7700
nn29 
2430
25class Factory:31class Factory:
26    all_factories = []32    all_factories = []
2733
28    known_materials = {34    known_materials = {
29        'Concrete': Concrete,35        'Concrete': Concrete,
30        'Brick': Brick,36        'Brick': Brick,
31        'Stone': Stone,37        'Stone': Stone,
32        'Wood': Wood,38        'Wood': Wood,
33        'Steel': Steel39        'Steel': Steel
34        }40        }
3541
36    def __init__(self):42    def __init__(self):
37        self.created = []43        self.created = []
38        Factory.all_factories.append(self)44        Factory.all_factories.append(self)
3945
40    def __call__(self, *args, **kargs):46    def __call__(self, *args, **kargs):
41        if args and kargs or not args and not kargs:47        if args and kargs or not args and not kargs:
42            raise ValueError48            raise ValueError
4349
44        if kargs:50        if kargs:
45            result = []51            result = []
46            for material_name, mass in kargs.items():52            for material_name, mass in kargs.items():
47                if material_name not in Factory.known_materials:53                if material_name not in Factory.known_materials:
48                    raise ValueError(f"Unknown material: {material_name}")54                    raise ValueError(f"Unknown material: {material_name}")
4955
50                material = Factory.known_materials[material_name]56                material = Factory.known_materials[material_name]
51                self.created.append(Factory.known_materials[material_name](mass))57                self.created.append(Factory.known_materials[material_name](mass))
52                result.append(material(mass))58                result.append(material(mass))
n53           n59 
54            return tuple(result)60            return tuple(result)
5561
56        if args:62        if args:
57            for material in args:63            for material in args:
58                if material.__class__.__name__ not in Factory.known_materials:64                if material.__class__.__name__ not in Factory.known_materials:
n59                    raise ValueError(f"Unknown material")n65                    raise ValueError(f"Unknown material: {material_name}")
6066
61            for material in args:67            for material in args:
62                if material.used:68                if material.used:
63                    raise AssertionError69                    raise AssertionError
n64                n70 
65            for material in args:71            for material in args:
66                material.used = True72                material.used = True
n67           n73 
68            for material in self.created:74            for material in self.created:
69                for names in args:75                for names in args:
70                    if material.__class__.__name__ is names.__class__.__name__:76                    if material.__class__.__name__ is names.__class__.__name__:
71                        material.used = True77                        material.used = True
7278
73            material_name = []79            material_name = []
7480
75            for material in args:81            for material in args:
76                material_name.append(material.__class__.__name__)82                material_name.append(material.__class__.__name__)
7783
78            new_material_name = '_'.join(material_name)84            new_material_name = '_'.join(material_name)
7985
80            material_mass = sum(material.mass for material in args)86            material_mass = sum(material.mass for material in args)
8187
82            if new_material_name in Factory.known_materials:88            if new_material_name in Factory.known_materials:
83                self.created.append(Factory.known_materials[new_material_name](material_mass))89                self.created.append(Factory.known_materials[new_material_name](material_mass))
84                return Factory.known_materials[new_material_name](material_mass)90                return Factory.known_materials[new_material_name](material_mass)
8591
86            original_materials_names = new_material_name.split('_')92            original_materials_names = new_material_name.split('_')
8793
88            material_density = 094            material_density = 0
89            for material in original_materials_names:95            for material in original_materials_names:
90                material_density += Factory.known_materials[material].density96                material_density += Factory.known_materials[material].density
9197
n92            material_density/= len(original_materials_names)n98            material_density /= len(original_materials_names)
93     99 
94            new_material = type(new_material_name, (Material,), {100            new_material = type(new_material_name, (Material,), {
95                'density': material_density,101                'density': material_density,
n96                '__init__': lambda self, mass: Material.__init__(self, mass),  n102                '__init__': lambda self, mass: Material.__init__(self, mass),
97                'volume': property(lambda self: self.mass / self.density),103                'volume': property(lambda self: self.mass / self.density),
98                'used': False,104                'used': False,
99            })105            })
n100            n106 
101            Factory.known_materials[new_material_name] = new_material107            Factory.known_materials[new_material_name] = new_material
102            self.created.append(Factory.known_materials[new_material_name](material_mass))108            self.created.append(Factory.known_materials[new_material_name](material_mass))
103109
104            return new_material(material_mass)110            return new_material(material_mass)
105111
106    def get_total_volume(self):112    def get_total_volume(self):
107        total_volume = 0113        total_volume = 0
108        for materials in self.created:114        for materials in self.created:
109            if materials.used is False:115            if materials.used is False:
110                total_volume += materials.volume116                total_volume += materials.volume
111        return total_volume117        return total_volume
n112        n118 
113    def can_build(self, volume_needed):119    def can_build(self, volume_needed):
114        return self.get_total_volume() >= volume_needed120        return self.get_total_volume() >= volume_needed
115121
116    @classmethod122    @classmethod
117    def can_build_together(cls, volume_needed):123    def can_build_together(cls, volume_needed):
118        total_volume = 0124        total_volume = 0
119125
120        for cur in range(0, len(cls.all_factories)):126        for cur in range(0, len(cls.all_factories)):
n121            total_volume += cls.all_factories[cur].get_total_volume() n127            total_volume += cls.all_factories[cur].get_total_volume()
122        print(total_volume)
123        return total_volume >= volume_needed128        return total_volume >= volume_needed
t124    t
125factory1 = Factory()
126brick1, wood1 = factory1(Brick=2000, Wood=1200)
127print(brick1.volume)  # 1.0
128print(wood1.volume)  # 2.0
129brick_wood1 = factory1(brick1, wood1)
130print(brick_wood1.volume)  # 2.46...
131print(factory1.can_build(3))  # False
132 
133factory2 = Factory()
134brick2, wood2 = factory2(Brick=2000, Wood=1200)
135print(brick2.volume)  # 1.0
136print(wood2.volume)  # 2.0
137brick_wood2 = factory2(brick2, wood2)
138print(brick_wood2.volume)  # 2.46...
139print(factory2.can_build(3))  # False
140 
141print(Factory.can_build_together(3))  # True
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op