Домашни > Подаръци ще има за всички от сърце > Решения > Решението на Георги Балтиев

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

6 точки общо

12 успешни теста
8 неуспешни теста
Код

  1import re
  2
  3ID_REGEX = r'^[\s]*((\d)+)[\s]*$'
  4PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'
  5
  6
  7def register_error_raising(func):
  8    def decorate(self, *args, **kwargs):
  9        try:
 10            func(self, *args, **kwargs)
 11        except Exception as e:
 12            metadata = Kid.kids_dictionary[id(self)]
 13            metadata.naughty = True
 14            raise e
 15    return decorate
 16
 17
 18def register_initialized_object(func):
 19    def decorate(self, *args, **kwargs):
 20        func(self, *args, **kwargs)
 21        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))
 22    return decorate
 23
 24
 25class KidMetadata:
 26    def __init__(self, kid):
 27        self.ages_left = 5
 28        self.reference = kid
 29        self.naughty = False
 30
 31    def decrement(self):
 32        self.ages_left -= 1
 33
 34    def is_old(self):
 35        return self.ages_left <= 0
 36
 37    def hash(self):
 38        return hash(self.reference)
 39
 40
 41class Kid(type):
 42
 43    kids_dictionary = {}
 44
 45    def __new__(cls, *args, **kwargs):
 46        constructed_class = type.__new__(cls, *args, **kwargs)
 47        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))
 48        if not callable(constructed_class):
 49            raise NotImplementedError("и аз съм от сдс")
 50
 51        for attr in constructed_class.__dict__:
 52            if callable(getattr(constructed_class, attr)):
 53                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))
 54
 55        return constructed_class
 56
 57
 58def match_regex(regex, string):
 59    matches = re.search(regex, string, re.MULTILINE)
 60    return matches.groups()[0]
 61
 62
 63def get_most_popular_gift(presents_list):
 64    if not len(presents_list):
 65        return None
 66    return max(presents_list, key=presents_list.count)
 67
 68
 69def iterate_through_requests(santa_reference):
 70    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}
 71    Kid.kids_dictionary = updated_kids
 72
 73    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))
 74    if most_popular_gift is None:
 75        return
 76
 77    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))
 78    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes
 79
 80    for kid in kids_with_present_wishes:
 81        target_kid_metadata = Kid.kids_dictionary[kid]
 82        target_kid_metadata.decrement()
 83        if target_kid_metadata.naughty:
 84            target_kid_metadata.naughty = False
 85            target_kid_metadata.reference("coal")
 86        else:
 87            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])
 88            santa_reference.presents_dictionary.pop(kid)
 89
 90    for kid in kids_without_present_wishes:
 91        target_kid_metadata = Kid.kids_dictionary[kid]
 92        target_kid_metadata.decrement()
 93        if target_kid_metadata.naughty:
 94            target_kid_metadata.naughty = False
 95            target_kid_metadata.reference("coal")
 96        else:
 97            target_kid_metadata.reference(most_popular_gift)
 98
 99
100class Santa(metaclass=type):
101
102    santa_instance = None
103    presents_dictionary = {}
104
105    def __new__(cls):
106        if cls.santa_instance is None:
107            cls.santa_instance = object.__new__(cls)
108
109        return cls.santa_instance
110
111    def __matmul__(self, other):
112
113        if isinstance(other, str):
114            child_id = match_regex(ID_REGEX, other)
115            present_name = match_regex(PRESENT_REGEX, other)
116            self.presents_dictionary[child_id] = present_name
117        else:
118            raise TypeError("Unsupported type")
119
120    def __call__(self, kid, present):
121
122        present = match_regex(PRESENT_REGEX, present)
123        self.presents_dictionary[id(kid)] = present
124
125    def __iter__(self):
126        return iter(self.presents_dictionary.values())
127
128    def xmas(self):
129        iterate_through_requests(self)

.F.F....F.F.F...FFF.
======================================================================
FAIL: test_class_from_kid_without_call_dunder (test.TestKid.test_class_from_kid_without_call_dunder)
Test creating new class from Kid.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 18, in test_class_from_kid_without_call_dunder
with self.assertRaises(NotImplementedError):
AssertionError: NotImplementedError not raised

======================================================================
FAIL: test_call_and_mail_same_kid (test.TestSanta.test_call_and_mail_same_kid)
Test that calls and mails work for the same kid.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 71, in test_call_and_mail_same_kid
self.assertEqual(list(self.santa), ['toy1'])
AssertionError: Lists differ: ['toy1', 'toy1'] != ['toy1']

First list contains 1 additional elements.
First extra element 1:
'toy1'

- ['toy1', 'toy1']
+ ['toy1']

======================================================================
FAIL: test_signature_matching (test.TestSanta.test_signature_matching)
Test matching present in the letter / call.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 119, in test_signature_matching
self.assertEqual(kid2.SECRET_PRESENT, 'toy2')
AssertionError: 'toy1' != 'toy2'
- toy1
? ^
+ toy2
? ^

======================================================================
FAIL: test_xmass (test.TestSanta.test_xmass)
Test a simple Christmas case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 143, in test_xmass
self.assertEqual(kid3.SECRET_PRESENT, 'toy3')
AssertionError: 'toy1' != 'toy3'
- toy1
? ^
+ toy3
? ^

======================================================================
FAIL: test_xmass_kid_without_a_wish (test.TestSanta.test_xmass_kid_without_a_wish)
Test a Christmas with a kids that hasn't sent a wish.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 177, in test_xmass_kid_without_a_wish
self.assertEqual(kid3.SECRET_PRESENT, 'toy2')
AssertionError: 'toy1' != 'toy2'
- toy1
? ^
+ toy2
? ^

======================================================================
FAIL: test_xmass_private_with_error (test.TestSanta.test_xmass_private_with_error)
Test a Christmas with not-so-naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 240, in test_xmass_private_with_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
AssertionError: 'coal' != 'sirenie'
- coal
+ sirenie

======================================================================
FAIL: test_xmass_public_with_no_error (test.TestSanta.test_xmass_public_with_no_error)
Test a Christmas with not-so-naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 246, in test_xmass_public_with_no_error
self.assertEqual(kid1.public_without_error(), 42)
AssertionError: None != 42

======================================================================
FAIL: test_xmass_years_5_and_over (test.TestSanta.test_xmass_years_5_and_over)
Test with passing years with kid aged 5 and over.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 213, in test_xmass_years_5_and_over
self.assertEqual(kid1.SECRET_PRESENT, None)
AssertionError: 'toy' != None

----------------------------------------------------------------------
Ran 20 tests in 0.022s

FAILED (failures=8)

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

f1f1
2import re2import re
33
4ID_REGEX = r'^[\s]*((\d)+)[\s]*$'4ID_REGEX = r'^[\s]*((\d)+)[\s]*$'
5PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'5PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'
66
77
8def register_error_raising(func):8def register_error_raising(func):
9    def decorate(self, *args, **kwargs):9    def decorate(self, *args, **kwargs):
10        try:10        try:
11            func(self, *args, **kwargs)11            func(self, *args, **kwargs)
12        except Exception as e:12        except Exception as e:
13            metadata = Kid.kids_dictionary[id(self)]13            metadata = Kid.kids_dictionary[id(self)]
14            metadata.naughty = True14            metadata.naughty = True
15            raise e15            raise e
16    return decorate16    return decorate
1717
1818
19def register_initialized_object(func):19def register_initialized_object(func):
20    def decorate(self, *args, **kwargs):20    def decorate(self, *args, **kwargs):
21        func(self, *args, **kwargs)21        func(self, *args, **kwargs)
22        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))22        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))
23    return decorate23    return decorate
2424
2525
26class KidMetadata:26class KidMetadata:
27    def __init__(self, kid):27    def __init__(self, kid):
28        self.ages_left = 528        self.ages_left = 5
29        self.reference = kid29        self.reference = kid
30        self.naughty = False30        self.naughty = False
3131
32    def decrement(self):32    def decrement(self):
33        self.ages_left -= 133        self.ages_left -= 1
3434
35    def is_old(self):35    def is_old(self):
36        return self.ages_left <= 036        return self.ages_left <= 0
3737
38    def hash(self):38    def hash(self):
39        return hash(self.reference)39        return hash(self.reference)
4040
4141
42class Kid(type):42class Kid(type):
4343
44    kids_dictionary = {}44    kids_dictionary = {}
4545
46    def __new__(cls, *args, **kwargs):46    def __new__(cls, *args, **kwargs):
47        constructed_class = type.__new__(cls, *args, **kwargs)47        constructed_class = type.__new__(cls, *args, **kwargs)
48        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))48        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))
49        if not callable(constructed_class):49        if not callable(constructed_class):
50            raise NotImplementedError("и аз съм от сдс")50            raise NotImplementedError("и аз съм от сдс")
5151
52        for attr in constructed_class.__dict__:52        for attr in constructed_class.__dict__:
53            if callable(getattr(constructed_class, attr)):53            if callable(getattr(constructed_class, attr)):
54                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))54                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))
5555
56        return constructed_class56        return constructed_class
5757
5858
59def match_regex(regex, string):59def match_regex(regex, string):
60    matches = re.search(regex, string, re.MULTILINE)60    matches = re.search(regex, string, re.MULTILINE)
61    return matches.groups()[0]61    return matches.groups()[0]
6262
6363
64def get_most_popular_gift(presents_list):64def get_most_popular_gift(presents_list):
65    if not len(presents_list):65    if not len(presents_list):
66        return None66        return None
67    return max(presents_list, key=presents_list.count)67    return max(presents_list, key=presents_list.count)
6868
6969
70def iterate_through_requests(santa_reference):70def iterate_through_requests(santa_reference):
71    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}71    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}
72    Kid.kids_dictionary = updated_kids72    Kid.kids_dictionary = updated_kids
7373
74    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))74    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))
75    if most_popular_gift is None:75    if most_popular_gift is None:
76        return76        return
7777
78    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))78    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))
79    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes79    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes
8080
81    for kid in kids_with_present_wishes:81    for kid in kids_with_present_wishes:
82        target_kid_metadata = Kid.kids_dictionary[kid]82        target_kid_metadata = Kid.kids_dictionary[kid]
83        target_kid_metadata.decrement()83        target_kid_metadata.decrement()
84        if target_kid_metadata.naughty:84        if target_kid_metadata.naughty:
85            target_kid_metadata.naughty = False85            target_kid_metadata.naughty = False
86            target_kid_metadata.reference("coal")86            target_kid_metadata.reference("coal")
87        else:87        else:
88            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])88            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])
89            santa_reference.presents_dictionary.pop(kid)89            santa_reference.presents_dictionary.pop(kid)
9090
91    for kid in kids_without_present_wishes:91    for kid in kids_without_present_wishes:
92        target_kid_metadata = Kid.kids_dictionary[kid]92        target_kid_metadata = Kid.kids_dictionary[kid]
93        target_kid_metadata.decrement()93        target_kid_metadata.decrement()
94        if target_kid_metadata.naughty:94        if target_kid_metadata.naughty:
95            target_kid_metadata.naughty = False95            target_kid_metadata.naughty = False
96            target_kid_metadata.reference("coal")96            target_kid_metadata.reference("coal")
97        else:97        else:
98            target_kid_metadata.reference(most_popular_gift)98            target_kid_metadata.reference(most_popular_gift)
9999
100100
101class Santa(metaclass=type):101class Santa(metaclass=type):
102102
103    santa_instance = None103    santa_instance = None
104    presents_dictionary = {}104    presents_dictionary = {}
105105
106    def __new__(cls):106    def __new__(cls):
107        if cls.santa_instance is None:107        if cls.santa_instance is None:
108            cls.santa_instance = object.__new__(cls)108            cls.santa_instance = object.__new__(cls)
109109
110        return cls.santa_instance110        return cls.santa_instance
111111
112    def __matmul__(self, other):112    def __matmul__(self, other):
113113
114        if isinstance(other, str):114        if isinstance(other, str):
115            child_id = match_regex(ID_REGEX, other)115            child_id = match_regex(ID_REGEX, other)
116            present_name = match_regex(PRESENT_REGEX, other)116            present_name = match_regex(PRESENT_REGEX, other)
117            self.presents_dictionary[child_id] = present_name117            self.presents_dictionary[child_id] = present_name
118        else:118        else:
119            raise TypeError("Unsupported type")119            raise TypeError("Unsupported type")
120120
121    def __call__(self, kid, present):121    def __call__(self, kid, present):
122122
123        present = match_regex(PRESENT_REGEX, present)123        present = match_regex(PRESENT_REGEX, present)
124        self.presents_dictionary[id(kid)] = present124        self.presents_dictionary[id(kid)] = present
125125
126    def __iter__(self):126    def __iter__(self):
127        return iter(self.presents_dictionary.values())127        return iter(self.presents_dictionary.values())
128128
129    def xmas(self):129    def xmas(self):
130        iterate_through_requests(self)130        iterate_through_requests(self)
t131 t
132 
133class BulgarianKid(metaclass=Kid):
134    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2import re2import re
t3from webbrowser import registert
43
5ID_REGEX = r'^[\s]*((\d)+)[\s]*$'4ID_REGEX = r'^[\s]*((\d)+)[\s]*$'
6PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'5PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'
76
87
9def register_error_raising(func):8def register_error_raising(func):
10    def decorate(self, *args, **kwargs):9    def decorate(self, *args, **kwargs):
11        try:10        try:
12            func(self, *args, **kwargs)11            func(self, *args, **kwargs)
13        except Exception as e:12        except Exception as e:
14            metadata = Kid.kids_dictionary[id(self)]13            metadata = Kid.kids_dictionary[id(self)]
15            metadata.naughty = True14            metadata.naughty = True
16            raise e15            raise e
17    return decorate16    return decorate
1817
1918
20def register_initialized_object(func):19def register_initialized_object(func):
21    def decorate(self, *args, **kwargs):20    def decorate(self, *args, **kwargs):
22        func(self, *args, **kwargs)21        func(self, *args, **kwargs)
23        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))22        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))
24    return decorate23    return decorate
2524
2625
27class KidMetadata:26class KidMetadata:
28    def __init__(self, kid):27    def __init__(self, kid):
29        self.ages_left = 528        self.ages_left = 5
30        self.reference = kid29        self.reference = kid
31        self.naughty = False30        self.naughty = False
3231
33    def decrement(self):32    def decrement(self):
34        self.ages_left -= 133        self.ages_left -= 1
3534
36    def is_old(self):35    def is_old(self):
37        return self.ages_left <= 036        return self.ages_left <= 0
3837
39    def hash(self):38    def hash(self):
40        return hash(self.reference)39        return hash(self.reference)
4140
4241
43class Kid(type):42class Kid(type):
4443
45    kids_dictionary = {}44    kids_dictionary = {}
4645
47    def __new__(cls, *args, **kwargs):46    def __new__(cls, *args, **kwargs):
48        constructed_class = type.__new__(cls, *args, **kwargs)47        constructed_class = type.__new__(cls, *args, **kwargs)
49        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))48        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))
50        if not callable(constructed_class):49        if not callable(constructed_class):
51            raise NotImplementedError("и аз съм от сдс")50            raise NotImplementedError("и аз съм от сдс")
5251
53        for attr in constructed_class.__dict__:52        for attr in constructed_class.__dict__:
54            if callable(getattr(constructed_class, attr)):53            if callable(getattr(constructed_class, attr)):
55                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))54                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))
5655
57        return constructed_class56        return constructed_class
5857
5958
60def match_regex(regex, string):59def match_regex(regex, string):
61    matches = re.search(regex, string, re.MULTILINE)60    matches = re.search(regex, string, re.MULTILINE)
62    return matches.groups()[0]61    return matches.groups()[0]
6362
6463
65def get_most_popular_gift(presents_list):64def get_most_popular_gift(presents_list):
66    if not len(presents_list):65    if not len(presents_list):
67        return None66        return None
68    return max(presents_list, key=presents_list.count)67    return max(presents_list, key=presents_list.count)
6968
7069
71def iterate_through_requests(santa_reference):70def iterate_through_requests(santa_reference):
72    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}71    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}
73    Kid.kids_dictionary = updated_kids72    Kid.kids_dictionary = updated_kids
7473
75    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))74    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))
76    if most_popular_gift is None:75    if most_popular_gift is None:
77        return76        return
7877
79    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))78    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))
80    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes79    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes
8180
82    for kid in kids_with_present_wishes:81    for kid in kids_with_present_wishes:
83        target_kid_metadata = Kid.kids_dictionary[kid]82        target_kid_metadata = Kid.kids_dictionary[kid]
84        target_kid_metadata.decrement()83        target_kid_metadata.decrement()
85        if target_kid_metadata.naughty:84        if target_kid_metadata.naughty:
86            target_kid_metadata.naughty = False85            target_kid_metadata.naughty = False
87            target_kid_metadata.reference("coal")86            target_kid_metadata.reference("coal")
88        else:87        else:
89            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])88            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])
90            santa_reference.presents_dictionary.pop(kid)89            santa_reference.presents_dictionary.pop(kid)
9190
92    for kid in kids_without_present_wishes:91    for kid in kids_without_present_wishes:
93        target_kid_metadata = Kid.kids_dictionary[kid]92        target_kid_metadata = Kid.kids_dictionary[kid]
94        target_kid_metadata.decrement()93        target_kid_metadata.decrement()
95        if target_kid_metadata.naughty:94        if target_kid_metadata.naughty:
96            target_kid_metadata.naughty = False95            target_kid_metadata.naughty = False
97            target_kid_metadata.reference("coal")96            target_kid_metadata.reference("coal")
98        else:97        else:
99            target_kid_metadata.reference(most_popular_gift)98            target_kid_metadata.reference(most_popular_gift)
10099
101100
102class Santa(metaclass=type):101class Santa(metaclass=type):
103102
104    santa_instance = None103    santa_instance = None
105    presents_dictionary = {}104    presents_dictionary = {}
106105
107    def __new__(cls):106    def __new__(cls):
108        if cls.santa_instance is None:107        if cls.santa_instance is None:
109            cls.santa_instance = object.__new__(cls)108            cls.santa_instance = object.__new__(cls)
110109
111        return cls.santa_instance110        return cls.santa_instance
112111
113    def __matmul__(self, other):112    def __matmul__(self, other):
114113
115        if isinstance(other, str):114        if isinstance(other, str):
116            child_id = match_regex(ID_REGEX, other)115            child_id = match_regex(ID_REGEX, other)
117            present_name = match_regex(PRESENT_REGEX, other)116            present_name = match_regex(PRESENT_REGEX, other)
118            self.presents_dictionary[child_id] = present_name117            self.presents_dictionary[child_id] = present_name
119        else:118        else:
120            raise TypeError("Unsupported type")119            raise TypeError("Unsupported type")
121120
122    def __call__(self, kid, present):121    def __call__(self, kid, present):
123122
124        present = match_regex(PRESENT_REGEX, present)123        present = match_regex(PRESENT_REGEX, present)
125        self.presents_dictionary[id(kid)] = present124        self.presents_dictionary[id(kid)] = present
126125
127    def __iter__(self):126    def __iter__(self):
128        return iter(self.presents_dictionary.values())127        return iter(self.presents_dictionary.values())
129128
130    def xmas(self):129    def xmas(self):
131        iterate_through_requests(self)130        iterate_through_requests(self)
132131
133132
134class BulgarianKid(metaclass=Kid):133class BulgarianKid(metaclass=Kid):
135    pass134    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
2import re2import re
nn3from webbrowser import register
34
4ID_REGEX = r'^[\s]*((\d)+)[\s]*$'5ID_REGEX = r'^[\s]*((\d)+)[\s]*$'
5PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'6PRESENT_REGEX = r'[\'|\"]([a-zA-Z0-9 ]+)[\'|\"]'
67
78
8def register_error_raising(func):9def register_error_raising(func):
9    def decorate(self, *args, **kwargs):10    def decorate(self, *args, **kwargs):
10        try:11        try:
11            func(self, *args, **kwargs)12            func(self, *args, **kwargs)
12        except Exception as e:13        except Exception as e:
n13            metadata = Kid.kids_dictionary[self]n14            metadata = Kid.kids_dictionary[id(self)]
14            metadata.naughty = True15            metadata.naughty = True
n15            Kid.kids_dictionary[self] = metadatan
16            raise e16            raise e
nn17    return decorate
18 
19 
20def register_initialized_object(func):
21    def decorate(self, *args, **kwargs):
22        func(self, *args, **kwargs)
23        Kid.kids_dictionary.__setitem__(id(self), KidMetadata(self))
17    return decorate24    return decorate
1825
1926
20class KidMetadata:27class KidMetadata:
21    def __init__(self, kid):28    def __init__(self, kid):
22        self.ages_left = 529        self.ages_left = 5
23        self.reference = kid30        self.reference = kid
24        self.naughty = False31        self.naughty = False
2532
26    def decrement(self):33    def decrement(self):
27        self.ages_left -= 134        self.ages_left -= 1
2835
29    def is_old(self):36    def is_old(self):
n30        return self.ages_left == -1n37        return self.ages_left <= 0
3138
32    def hash(self):39    def hash(self):
33        return hash(self.reference)40        return hash(self.reference)
3441
3542
36class Kid(type):43class Kid(type):
3744
38    kids_dictionary = {}45    kids_dictionary = {}
3946
40    def __new__(cls, *args, **kwargs):47    def __new__(cls, *args, **kwargs):
41        constructed_class = type.__new__(cls, *args, **kwargs)48        constructed_class = type.__new__(cls, *args, **kwargs)
n42        constructed_class.__init__ = lambda self: cls.kids_dictionary.__setitem__(id(self), KidMetadata(self))n49        constructed_class.__init__ = register_initialized_object(getattr(constructed_class, "__init__"))
43 50        if not callable(constructed_class):
44        try:51            raise NotImplementedError("и аз съм от сдс")
45            constructed_class.__dict__["__call__"]
46        except KeyError:
47            raise NotImplementedError("Not implemented")
4852
49        for attr in constructed_class.__dict__:53        for attr in constructed_class.__dict__:
50            if callable(getattr(constructed_class, attr)):54            if callable(getattr(constructed_class, attr)):
51                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))55                setattr(constructed_class, attr, register_error_raising(getattr(constructed_class, attr)))
5256
53        return constructed_class57        return constructed_class
5458
5559
56def match_regex(regex, string):60def match_regex(regex, string):
57    matches = re.search(regex, string, re.MULTILINE)61    matches = re.search(regex, string, re.MULTILINE)
58    return matches.groups()[0]62    return matches.groups()[0]
5963
6064
n61def get_most_popular_gift(santa_iterator):n65def get_most_popular_gift(presents_list):
62    max_occurrences = 066    if not len(presents_list):
63    occurrences_dict = {}
64    for gift in santa_iterator:
65        current_occurrences = occurrences_dict.get(gift, 0) + 1
66        occurrences_dict[gift] = current_occurrences
67        max_occurrences = max(max_occurrences, current_occurrences)
68 
69    if len(occurrences_dict) == 0:
70        return None67        return None
n71 n68    return max(presents_list, key=presents_list.count)
72    popular_gifts = {gift: occurrence for (gift, occurrence) in occurrences_dict.items() if occurrence == max_occurrences}
73    return next(iter(popular_gifts))
7469
7570
76def iterate_through_requests(santa_reference):71def iterate_through_requests(santa_reference):
77    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}72    updated_kids = {child: metadata for (child, metadata) in Kid.kids_dictionary.items() if metadata.ages_left >= 0}
78    Kid.kids_dictionary = updated_kids73    Kid.kids_dictionary = updated_kids
7974
n80    most_popular_gift = get_most_popular_gift(iter(santa_reference))n75    most_popular_gift = get_most_popular_gift(list(santa_reference.presents_dictionary.values()))
81    if most_popular_gift is None:76    if most_popular_gift is None:
82        return77        return
8378
n84    kids_with_present_wishes = santa_reference.presents_dictionary.keys().intersection(Kid.kids_dictionary.keys())n79    kids_with_present_wishes = list(set(santa_reference.presents_dictionary.keys()).intersection(set(Kid.kids_dictionary.keys())))
85    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes80    kids_without_present_wishes = Kid.kids_dictionary.keys() - kids_with_present_wishes
8681
87    for kid in kids_with_present_wishes:82    for kid in kids_with_present_wishes:
88        target_kid_metadata = Kid.kids_dictionary[kid]83        target_kid_metadata = Kid.kids_dictionary[kid]
89        target_kid_metadata.decrement()84        target_kid_metadata.decrement()
90        if target_kid_metadata.naughty:85        if target_kid_metadata.naughty:
91            target_kid_metadata.naughty = False86            target_kid_metadata.naughty = False
nn87            target_kid_metadata.reference("coal")
92        else:88        else:
nn89            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])
93            santa_reference.presents_dictionary.pop(kid)90            santa_reference.presents_dictionary.pop(kid)
n94            target_kid_metadata.reference(santa_reference.presents_dictionary[kid])n
9591
96    for kid in kids_without_present_wishes:92    for kid in kids_without_present_wishes:
97        target_kid_metadata = Kid.kids_dictionary[kid]93        target_kid_metadata = Kid.kids_dictionary[kid]
98        target_kid_metadata.decrement()94        target_kid_metadata.decrement()
99        if target_kid_metadata.naughty:95        if target_kid_metadata.naughty:
100            target_kid_metadata.naughty = False96            target_kid_metadata.naughty = False
nn97            target_kid_metadata.reference("coal")
101        else:98        else:
102            target_kid_metadata.reference(most_popular_gift)99            target_kid_metadata.reference(most_popular_gift)
103100
104101
105class Santa(metaclass=type):102class Santa(metaclass=type):
106103
107    santa_instance = None104    santa_instance = None
108    presents_dictionary = {}105    presents_dictionary = {}
109106
110    def __new__(cls):107    def __new__(cls):
n111 n
112        if cls.santa_instance is None:108        if cls.santa_instance is None:
113            cls.santa_instance = object.__new__(cls)109            cls.santa_instance = object.__new__(cls)
114110
115        return cls.santa_instance111        return cls.santa_instance
116112
117    def __matmul__(self, other):113    def __matmul__(self, other):
118114
119        if isinstance(other, str):115        if isinstance(other, str):
120            child_id = match_regex(ID_REGEX, other)116            child_id = match_regex(ID_REGEX, other)
121            present_name = match_regex(PRESENT_REGEX, other)117            present_name = match_regex(PRESENT_REGEX, other)
122            self.presents_dictionary[child_id] = present_name118            self.presents_dictionary[child_id] = present_name
123        else:119        else:
124            raise TypeError("Unsupported type")120            raise TypeError("Unsupported type")
125121
n126    def __call__(self, *args, **kwargs):n122    def __call__(self, kid, present):
127123
n128        if len(args) != 2 or len(kwargs) > 0:n
129            raise ValueError("Invalid arguments for a letter")
130 
131        present = match_regex(PRESENT_REGEX, args[1])124        present = match_regex(PRESENT_REGEX, present)
132        self.presents_dictionary[id(args[0])] = present125        self.presents_dictionary[id(kid)] = present
133126
134    def __iter__(self):127    def __iter__(self):
135        return iter(self.presents_dictionary.values())128        return iter(self.presents_dictionary.values())
136129
137    def xmas(self):130    def xmas(self):
138        iterate_through_requests(self)131        iterate_through_requests(self)
tt132 
133 
134class BulgarianKid(metaclass=Kid):
135    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op