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

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

11 точки общо

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

  1BASE_MATERIAL_DENSITIES = {
  2    'Concrete': 2500,
  3    'Brick': 2000,
  4    'Stone': 1600,
  5    'Wood': 600,
  6    'Steel': 7700
  7}
  8
  9classes = {}
 10
 11
 12def initializer_with_density(density):
 13    def instance_initializer_method(instance_reference, mass):
 14        setattr(instance_reference, "mass", mass)
 15        setattr(instance_reference, "volume", mass / density)
 16        setattr(instance_reference, "used", False)
 17    return instance_initializer_method
 18
 19
 20class Material:
 21    def __init__(self, mass):
 22        self.mass = mass
 23        self.used = False
 24
 25
 26def class_generator(*class_names_list):
 27
 28    class_name = "_".join(class_names_list)
 29
 30    if class_name in classes:
 31        return classes[class_name]
 32
 33    if class_name in BASE_MATERIAL_DENSITIES:
 34        density = BASE_MATERIAL_DENSITIES[class_name]
 35    else:
 36        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))
 37
 38    classes[class_name] = type(class_name, (Material,), {'volume': property(lambda self: self.mass / density)})
 39    return classes[class_name]
 40
 41
 42Concrete = class_generator('Concrete')
 43Brick = class_generator('Brick')
 44Stone = class_generator('Stone')
 45Steel = class_generator('Steel')
 46Wood = class_generator('Wood')
 47
 48
 49class Factory:
 50
 51    __factory_instances = []
 52
 53    def __init__(self):
 54        self.__factory_instances.append(self)
 55        self.__created_materials = []
 56
 57    def __del__(self):
 58        self.__factory_instances.remove(self)
 59
 60    def __call_with_existing_materials(self, *existing_materials):
 61        if any(map(lambda material: material.used, existing_materials)):
 62            raise AssertionError
 63        else:
 64            for material in existing_materials:
 65                material.used = True
 66
 67        sorted_names = sum(list(map(lambda class_name: class_name.split('_'),
 68                                    map(lambda material: material.__class__.__name__, existing_materials))), [])
 69        sorted_names.sort()
 70        obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))
 71        self.__created_materials.append(obj)
 72        return obj
 73
 74    def __call_with_new_materials(self, **new_materials):
 75        newly_created_materials = []
 76        for material_name, mass in new_materials.items():
 77            if material_name in classes:
 78                obj = classes[material_name](mass)
 79                self.__created_materials.append(obj)
 80                newly_created_materials.append(obj)
 81            else:
 82                raise ValueError
 83
 84        return tuple(newly_created_materials)
 85
 86    def __call__(self, *existing_materials, **new_materials):
 87        if not (len(existing_materials) ^ len(new_materials)):
 88            raise ValueError
 89
 90        if len(existing_materials) > 0:
 91            return self.__call_with_existing_materials(*existing_materials)
 92
 93        if len(new_materials) > 0:
 94            return self.__call_with_new_materials(**new_materials)
 95
 96    @property
 97    def volume(self):
 98        return sum(map(lambda ref: ref.volume,
 99                       filter(lambda ref: not ref.used, self.__created_materials)))
100
101    @classmethod
102    def can_build_together(cls, desired_volume):
103        return sum(map(lambda ref: ref.volume, cls.__factory_instances)) >= desired_volume
104
105    def can_build(self, desired_volume):
106        return self.volume >= desired_volume

..........
----------------------------------------------------------------------
Ran 10 tests in 0.014s

OK

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

f1f1
2BASE_MATERIAL_DENSITIES = {2BASE_MATERIAL_DENSITIES = {
3    'Concrete': 2500,3    'Concrete': 2500,
4    'Brick': 2000,4    'Brick': 2000,
5    'Stone': 1600,5    'Stone': 1600,
6    'Wood': 600,6    'Wood': 600,
7    'Steel': 77007    'Steel': 7700
8}8}
99
n10CLASSES = {}n10classes = {}
1111
1212
13def initializer_with_density(density):13def initializer_with_density(density):
14    def instance_initializer_method(instance_reference, mass):14    def instance_initializer_method(instance_reference, mass):
15        setattr(instance_reference, "mass", mass)15        setattr(instance_reference, "mass", mass)
16        setattr(instance_reference, "volume", mass / density)16        setattr(instance_reference, "volume", mass / density)
17        setattr(instance_reference, "used", False)17        setattr(instance_reference, "used", False)
18    return instance_initializer_method18    return instance_initializer_method
1919
2020
nn21class Material:
22    def __init__(self, mass):
23        self.mass = mass
24        self.used = False
25 
26 
21def class_generator(*class_names_list):27def class_generator(*class_names_list):
2228
23    class_name = "_".join(class_names_list)29    class_name = "_".join(class_names_list)
2430
n25    if class_name in CLASSES:n31    if class_name in classes:
26        return CLASSES[class_name]32        return classes[class_name]
2733
28    if class_name in BASE_MATERIAL_DENSITIES:34    if class_name in BASE_MATERIAL_DENSITIES:
29        density = BASE_MATERIAL_DENSITIES[class_name]35        density = BASE_MATERIAL_DENSITIES[class_name]
30    else:36    else:
31        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))37        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))
3238
n33    CLASSES[class_name] = type(class_name, (), {'__init__': initializer_with_density(density)})n39    classes[class_name] = type(class_name, (Material,), {'volume': property(lambda self: self.mass / density)})
34    return CLASSES[class_name]40    return classes[class_name]
3541
3642
37Concrete = class_generator('Concrete')43Concrete = class_generator('Concrete')
38Brick = class_generator('Brick')44Brick = class_generator('Brick')
39Stone = class_generator('Stone')45Stone = class_generator('Stone')
40Steel = class_generator('Steel')46Steel = class_generator('Steel')
41Wood = class_generator('Wood')47Wood = class_generator('Wood')
4248
4349
44class Factory:50class Factory:
4551
n46    __factory_instances_list = list()n52    __factory_instances = []
4753
48    def __init__(self):54    def __init__(self):
n49        self.__factory_instances_list.append(self)n55        self.__factory_instances.append(self)
50        self.__created_materials_list = list()56        self.__created_materials = []
5157
52    def __del__(self):58    def __del__(self):
n53        self.__factory_instances_list.remove(self)n59        self.__factory_instances.remove(self)
5460
nn61    def __call_with_existing_materials(self, *existing_materials):
62        if any(map(lambda material: material.used, existing_materials)):
63            raise AssertionError
64        else:
65            for material in existing_materials:
66                material.used = True
67 
68        sorted_names = sum(list(map(lambda class_name: class_name.split('_'),
69                                    map(lambda material: material.__class__.__name__, existing_materials))), [])
70        sorted_names.sort()
71        obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))
72        self.__created_materials.append(obj)
73        return obj
74 
75    def __call_with_new_materials(self, **new_materials):
76        newly_created_materials = []
77        for material_name, mass in new_materials.items():
78            if material_name in classes:
79                obj = classes[material_name](mass)
80                self.__created_materials.append(obj)
81                newly_created_materials.append(obj)
82            else:
83                raise ValueError
84 
85        return tuple(newly_created_materials)
86 
87    def __call__(self, *existing_materials, **new_materials):
88        if not (len(existing_materials) ^ len(new_materials)):
89            raise ValueError
90 
91        if len(existing_materials) > 0:
92            return self.__call_with_existing_materials(*existing_materials)
93 
94        if len(new_materials) > 0:
95            return self.__call_with_new_materials(**new_materials)
96 
97    @property
55    def __get_volume(self):98    def volume(self):
56        return sum(map(lambda ref: ref.volume,99        return sum(map(lambda ref: ref.volume,
n57                       filter(lambda ref: not ref.used, self.__created_materials_list)))n100                       filter(lambda ref: not ref.used, self.__created_materials)))
58101
59    @classmethod102    @classmethod
60    def can_build_together(cls, desired_volume):103    def can_build_together(cls, desired_volume):
n61        return sum(map(lambda ref: ref.__get_volume(), cls.__factory_instances_list)) >= desired_volumen104        return sum(map(lambda ref: ref.volume, cls.__factory_instances)) >= desired_volume
62105
63    def can_build(self, desired_volume):106    def can_build(self, desired_volume):
t64        return self.__get_volume() >= desired_volumet107        return self.volume >= desired_volume
65 
66    def __call__(self, *existing_materials, **new_materials):
67        if ((len(existing_materials) == 0 and len(new_materials) == 0) or
68                (len(existing_materials) > 0 and len(new_materials) > 0)):
69            raise ValueError
70 
71        if len(existing_materials) > 0:
72            if any(map(lambda material: material.used, existing_materials)):
73                raise AssertionError
74            else:
75                for material in existing_materials:
76                    material.used = True
77 
78            sorted_names = sum(list(map(lambda class_name: class_name.split('_'),
79                                    map(lambda material: material.__class__.__name__, existing_materials))), [])
80            sorted_names.sort()
81            obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))
82            self.__created_materials_list.append(obj)
83            return obj
84 
85        if len(new_materials) > 0:
86            newly_created_materials = []
87            for material_name, mass in new_materials.items():
88                if material_name in CLASSES:
89                    obj = CLASSES[material_name](mass)
90                    self.__created_materials_list.append(obj)
91                    newly_created_materials.append(obj)
92                else:
93                    raise ValueError
94 
95            return tuple(newly_created_materials)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2BASE_MATERIAL_DENSITIES = {2BASE_MATERIAL_DENSITIES = {
3    'Concrete': 2500,3    'Concrete': 2500,
4    'Brick': 2000,4    'Brick': 2000,
5    'Stone': 1600,5    'Stone': 1600,
6    'Wood': 600,6    'Wood': 600,
7    'Steel': 77007    'Steel': 7700
8}8}
99
10CLASSES = {}10CLASSES = {}
1111
1212
13def initializer_with_density(density):13def initializer_with_density(density):
14    def instance_initializer_method(instance_reference, mass):14    def instance_initializer_method(instance_reference, mass):
15        setattr(instance_reference, "mass", mass)15        setattr(instance_reference, "mass", mass)
16        setattr(instance_reference, "volume", mass / density)16        setattr(instance_reference, "volume", mass / density)
17        setattr(instance_reference, "used", False)17        setattr(instance_reference, "used", False)
18    return instance_initializer_method18    return instance_initializer_method
1919
2020
21def class_generator(*class_names_list):21def class_generator(*class_names_list):
2222
23    class_name = "_".join(class_names_list)23    class_name = "_".join(class_names_list)
2424
25    if class_name in CLASSES:25    if class_name in CLASSES:
26        return CLASSES[class_name]26        return CLASSES[class_name]
2727
28    if class_name in BASE_MATERIAL_DENSITIES:28    if class_name in BASE_MATERIAL_DENSITIES:
29        density = BASE_MATERIAL_DENSITIES[class_name]29        density = BASE_MATERIAL_DENSITIES[class_name]
30    else:30    else:
31        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))31        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))
3232
33    CLASSES[class_name] = type(class_name, (), {'__init__': initializer_with_density(density)})33    CLASSES[class_name] = type(class_name, (), {'__init__': initializer_with_density(density)})
34    return CLASSES[class_name]34    return CLASSES[class_name]
3535
3636
37Concrete = class_generator('Concrete')37Concrete = class_generator('Concrete')
38Brick = class_generator('Brick')38Brick = class_generator('Brick')
39Stone = class_generator('Stone')39Stone = class_generator('Stone')
40Steel = class_generator('Steel')40Steel = class_generator('Steel')
41Wood = class_generator('Wood')41Wood = class_generator('Wood')
4242
4343
44class Factory:44class Factory:
4545
46    __factory_instances_list = list()46    __factory_instances_list = list()
4747
48    def __init__(self):48    def __init__(self):
49        self.__factory_instances_list.append(self)49        self.__factory_instances_list.append(self)
50        self.__created_materials_list = list()50        self.__created_materials_list = list()
5151
52    def __del__(self):52    def __del__(self):
53        self.__factory_instances_list.remove(self)53        self.__factory_instances_list.remove(self)
5454
55    def __get_volume(self):55    def __get_volume(self):
n56        return sum(map(lambda ref: ref.volume, filter(lambda ref: not ref.used, self.__created_materials_list)))n56        return sum(map(lambda ref: ref.volume,
57                       filter(lambda ref: not ref.used, self.__created_materials_list)))
5758
58    @classmethod59    @classmethod
59    def can_build_together(cls, desired_volume):60    def can_build_together(cls, desired_volume):
60        return sum(map(lambda ref: ref.__get_volume(), cls.__factory_instances_list)) >= desired_volume61        return sum(map(lambda ref: ref.__get_volume(), cls.__factory_instances_list)) >= desired_volume
6162
62    def can_build(self, desired_volume):63    def can_build(self, desired_volume):
63        return self.__get_volume() >= desired_volume64        return self.__get_volume() >= desired_volume
6465
65    def __call__(self, *existing_materials, **new_materials):66    def __call__(self, *existing_materials, **new_materials):
n66        if (len(existing_materials) == 0 and len(new_materials) == 0) or (len(existing_materials) > 0 and len(new_materials) > 0):n67        if ((len(existing_materials) == 0 and len(new_materials) == 0) or
68                (len(existing_materials) > 0 and len(new_materials) > 0)):
67            raise ValueError69            raise ValueError
6870
69        if len(existing_materials) > 0:71        if len(existing_materials) > 0:
70            if any(map(lambda material: material.used, existing_materials)):72            if any(map(lambda material: material.used, existing_materials)):
71                raise AssertionError73                raise AssertionError
72            else:74            else:
73                for material in existing_materials:75                for material in existing_materials:
74                    material.used = True76                    material.used = True
7577
t76            sorted_names = sum(list(map(lambda class_name: class_name.split('_'), map(lambda material: material.__class__.__name__, existing_materials))), [])t78            sorted_names = sum(list(map(lambda class_name: class_name.split('_'),
79                                    map(lambda material: material.__class__.__name__, existing_materials))), [])
77            sorted_names.sort()80            sorted_names.sort()
78            obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))81            obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))
79            self.__created_materials_list.append(obj)82            self.__created_materials_list.append(obj)
80            return obj83            return obj
8184
82        if len(new_materials) > 0:85        if len(new_materials) > 0:
83            newly_created_materials = []86            newly_created_materials = []
84            for material_name, mass in new_materials.items():87            for material_name, mass in new_materials.items():
85                if material_name in CLASSES:88                if material_name in CLASSES:
86                    obj = CLASSES[material_name](mass)89                    obj = CLASSES[material_name](mass)
87                    self.__created_materials_list.append(obj)90                    self.__created_materials_list.append(obj)
88                    newly_created_materials.append(obj)91                    newly_created_materials.append(obj)
89                else:92                else:
90                    raise ValueError93                    raise ValueError
9194
92            return tuple(newly_created_materials)95            return tuple(newly_created_materials)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2BASE_MATERIAL_DENSITIES = {2BASE_MATERIAL_DENSITIES = {
3    'Concrete': 2500,3    'Concrete': 2500,
4    'Brick': 2000,4    'Brick': 2000,
5    'Stone': 1600,5    'Stone': 1600,
6    'Wood': 600,6    'Wood': 600,
7    'Steel': 77007    'Steel': 7700
8}8}
99
10CLASSES = {}10CLASSES = {}
1111
1212
13def initializer_with_density(density):13def initializer_with_density(density):
14    def instance_initializer_method(instance_reference, mass):14    def instance_initializer_method(instance_reference, mass):
15        setattr(instance_reference, "mass", mass)15        setattr(instance_reference, "mass", mass)
16        setattr(instance_reference, "volume", mass / density)16        setattr(instance_reference, "volume", mass / density)
17        setattr(instance_reference, "used", False)17        setattr(instance_reference, "used", False)
18    return instance_initializer_method18    return instance_initializer_method
1919
2020
21def class_generator(*class_names_list):21def class_generator(*class_names_list):
2222
23    class_name = "_".join(class_names_list)23    class_name = "_".join(class_names_list)
2424
25    if class_name in CLASSES:25    if class_name in CLASSES:
26        return CLASSES[class_name]26        return CLASSES[class_name]
2727
28    if class_name in BASE_MATERIAL_DENSITIES:28    if class_name in BASE_MATERIAL_DENSITIES:
29        density = BASE_MATERIAL_DENSITIES[class_name]29        density = BASE_MATERIAL_DENSITIES[class_name]
30    else:30    else:
31        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))31        density = sum(map(lambda cls: BASE_MATERIAL_DENSITIES[cls] / len(class_names_list), class_names_list))
3232
33    CLASSES[class_name] = type(class_name, (), {'__init__': initializer_with_density(density)})33    CLASSES[class_name] = type(class_name, (), {'__init__': initializer_with_density(density)})
34    return CLASSES[class_name]34    return CLASSES[class_name]
3535
3636
37Concrete = class_generator('Concrete')37Concrete = class_generator('Concrete')
38Brick = class_generator('Brick')38Brick = class_generator('Brick')
39Stone = class_generator('Stone')39Stone = class_generator('Stone')
40Steel = class_generator('Steel')40Steel = class_generator('Steel')
41Wood = class_generator('Wood')41Wood = class_generator('Wood')
4242
4343
44class Factory:44class Factory:
4545
n46    __instances_list = list()n46    __factory_instances_list = list()
4747
48    def __init__(self):48    def __init__(self):
n49        self.__instances_list.append(self)n49        self.__factory_instances_list.append(self)
50        self.__objects_list = list()50        self.__created_materials_list = list()
5151
52    def __del__(self):52    def __del__(self):
n53        self.__instances_list.remove(self)n53        self.__factory_instances_list.remove(self)
5454
55    def __get_volume(self):55    def __get_volume(self):
n56        return sum(map(lambda ref: ref.volume, filter(lambda ref: not ref.used, self.__objects_list)))n56        return sum(map(lambda ref: ref.volume, filter(lambda ref: not ref.used, self.__created_materials_list)))
5757
58    @classmethod58    @classmethod
59    def can_build_together(cls, desired_volume):59    def can_build_together(cls, desired_volume):
n60        return sum(map(lambda ref: ref.__get_volume(), cls.__instances_list)) >= desired_volumen60        return sum(map(lambda ref: ref.__get_volume(), cls.__factory_instances_list)) >= desired_volume
6161
62    def can_build(self, desired_volume):62    def can_build(self, desired_volume):
63        return self.__get_volume() >= desired_volume63        return self.__get_volume() >= desired_volume
6464
n65    def __call__(self, *args, **kwargs):n65    def __call__(self, *existing_materials, **new_materials):
66        if (len(args) == 0 and len(kwargs) == 0) or (len(args) > 0 and len(kwargs) > 0):66        if (len(existing_materials) == 0 and len(new_materials) == 0) or (len(existing_materials) > 0 and len(new_materials) > 0):
67            raise ValueError67            raise ValueError
6868
n69        if len(args) > 0:n69        if len(existing_materials) > 0:
70            if any(map(lambda arg: arg.used, args)):70            if any(map(lambda materialmaterial.used, existing_materials)):
71                raise AssertionError71                raise AssertionError
72            else:72            else:
n73                for arg in args:n73                for material in existing_materials:
74                    arg.used = True74                    material.used = True
7575
n76            sorted_arguments = sum(list(map(lambda cls: cls.split('_'), map(lambda keystr(key.__class__.__name__), args))), [])n76            sorted_names = sum(list(map(lambda class_name: class_name.split('_'), map(lambda materialmaterial.__class__.__name__, existing_materials))), [])
77            sorted_arguments.sort()77            sorted_names.sort()
78            obj = class_generator(*sorted_arguments)(sum(map(lambda object: object.mass, args)))78            obj = class_generator(*sorted_names)(sum(map(lambda material: material.mass, existing_materials)))
79            self.__objects_list.append(obj)79            self.__created_materials_list.append(obj)
80            return obj80            return obj
8181
n82        if len(kwargs) > 0:n82        if len(new_materials) > 0:
83            objects = []83            newly_created_materials = []
84            for key, value in kwargs.items():84            for material_name, mass in new_materials.items():
85                if key in CLASSES:85                if material_name in CLASSES:
86                    obj = CLASSES[key](value)86                    obj = CLASSES[material_name](mass)
87                    self.__objects_list.append(obj)87                    self.__created_materials_list.append(obj)
88                    objects.append(obj)88                    newly_created_materials.append(obj)
89                else:89                else:
90                    raise ValueError90                    raise ValueError
9191
t92            return tuple(objects)t92            return tuple(newly_created_materials)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op