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