1import re
2
3
4class Singleton(type):
5 __instances = {}
6
7 def __call__(cls, *args, **kwargs):
8 if not cls.__instances:
9 cls.__instances[cls] = super().__call__(*args, **kwargs)
10 return cls.__instances[cls]
11
12
13class Santa (metaclass=Singleton):
14 __wish_list = []
15 __children = []
16 __children_and_ages = {}
17 __children_and_wishes = {}
18 __most_desired = ''
19
20 def __call__(self, child, _wish):
21 wish = re.search(r'[\'\"]([\w\t])+[\'\"]', _wish)
22 self.__wish_list.append(wish)
23 if child.id() not in self.__children:
24 self.__children.append(child.id())
25 self.__children_and_ages[child.id()] = 1
26 self.__children_and_wishes[child.id()] = wish
27
28 def __matmul__(self, letter):
29 child_id = re.search('(\s*)([0-9])+(\s*)', letter)
30 wish = re.search(r'[\'\"]([\w\t])+[\'\"]', letter)
31 self.__wish_list.append(wish)
32 if child_id not in self.__children:
33 self.__children.append(child_id)
34 self.__children_and_ages[child_id] = 1
35 self.__children_and_wishes[child_id] = wish
36
37 def __iter__(self):
38 self.num = 0
39 return self
40
41 def __next__(self):
42 if self.num < len(self.__wish_list):
43 result = self.__wish_list[self.num]
44 self.num += 1
45 return result
46 else:
47 raise StopIteration
48
49 def __get_most_desired(self):
50 count = {}
51 for wish in self.__wish_list:
52 count[wish] = self.__wish_list.count(wish)
53 __most_desired = max(count, key=count.get)
54 self.__wish_list = []
55 self.__children = []
56 self.__children_and_wishes = {}
57
58 def xmas(self):
59 for child in self.__children:
60 if self.__children_and_ages[child] <= 5:
61 if child not in self.__children_and_wishes:
62 kid(self.__most_desired)
63 elif ():
64 kid(self.__children_and_wishes[child])
65 else:
66 kid('coal')
67 self.__children_and_ages[child] += 1
68 self.__get_most_desired()
69
70
71class Kid:
72 pass
/tmp/solution.py:29: SyntaxWarning: invalid escape sequence '\s'
child_id = re.search('(\s*)([0-9])+(\s*)', letter)
EEEEEEEEEEEEEEEEEEEE
======================================================================
ERROR: test_class_from_kid (test.TestKid.test_class_from_kid)
Test creating new class from Kid.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 13, in test_class_from_kid
cls = solution.Kid('name', (), {'__call__': print})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 19, in test_class_from_kid_without_call_dunder
solution.Kid('name', (), {})
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_call (test.TestSanta.test_call)
Test sending message via calling.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_iterable (test.TestSanta.test_iterable)
Ensure Santa can be iterated multiple times including overwriting presents.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_mail (test.TestSanta.test_mail)
Test sending message via email.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_present_matching (test.TestSanta.test_present_matching)
Test matching signature in the letter.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_santa_gift_order (test.TestSanta.test_santa_gift_order)
Test ordering of the Santa iterator.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_singleton (test.TestSanta.test_singleton)
Ensure Santa is a singleton.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass (test.TestSanta.test_xmass)
Test a simple Christmas case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass_kid_with_multiple_wishes (test.TestSanta.test_xmass_kid_with_multiple_wishes)
Test a Christmas with a kid who sends multiple wishes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass_naughty (test.TestSanta.test_xmass_naughty)
Test a Christmas with naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass_no_wishes (test.TestSanta.test_xmass_no_wishes)
Test a Christmas with no wishes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass_no_wishes_but_naughty_kids (test.TestSanta.test_xmass_no_wishes_but_naughty_kids)
Test a Christmas with no wishes, but naughty kids present.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: 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 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
======================================================================
ERROR: test_xmass_years_under_5 (test.TestSanta.test_xmass_years_under_5)
Test with passing years with a kid under 5 years old.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 35, in setUp
self.KidClass1 = solution.Kid('KidClass1', (), deepcopy(self.KID_DICT))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Kid() takes no arguments
----------------------------------------------------------------------
Ran 20 tests in 0.003s
FAILED (errors=20)
Виктор Бечев
19.12.2024 16:43Fair point.
Наследи `type` от `Kid`, за да можеш да си видиш грешните тестове, ако ти е интересно. Иначе пак бих ти дал точката, която ще имаш така или иначе _(уви, само със `Santa` няма как да ти минат повечето тестове)_, за поощрение, но пък така няма да видиш реалните причини да не ти минават тестовете.
|
Ева Банова
19.12.2024 16:15it ain't much but it's honest work :( <3
|
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | 3 | ||||
4 | class Singleton(type): | 4 | class Singleton(type): | ||
5 | __instances = {} | 5 | __instances = {} | ||
6 | 6 | ||||
7 | def __call__(cls, *args, **kwargs): | 7 | def __call__(cls, *args, **kwargs): | ||
8 | if not cls.__instances: | 8 | if not cls.__instances: | ||
9 | cls.__instances[cls] = super().__call__(*args, **kwargs) | 9 | cls.__instances[cls] = super().__call__(*args, **kwargs) | ||
10 | return cls.__instances[cls] | 10 | return cls.__instances[cls] | ||
11 | 11 | ||||
12 | 12 | ||||
13 | class Santa (metaclass=Singleton): | 13 | class Santa (metaclass=Singleton): | ||
14 | __wish_list = [] | 14 | __wish_list = [] | ||
15 | __children = [] | 15 | __children = [] | ||
16 | __children_and_ages = {} | 16 | __children_and_ages = {} | ||
17 | __children_and_wishes = {} | 17 | __children_and_wishes = {} | ||
18 | __most_desired = '' | 18 | __most_desired = '' | ||
19 | 19 | ||||
20 | def __call__(self, child, _wish): | 20 | def __call__(self, child, _wish): | ||
n | 21 | wish = re.search('[\'\"]([\w\t])+[\'\"]', _wish) | n | 21 | wish = re.search(r'[\'\"]([\w\t])+[\'\"]', _wish) |
22 | self.__wish_list.append(wish) | 22 | self.__wish_list.append(wish) | ||
23 | if child.id() not in self.__children: | 23 | if child.id() not in self.__children: | ||
24 | self.__children.append(child.id()) | 24 | self.__children.append(child.id()) | ||
25 | self.__children_and_ages[child.id()] = 1 | 25 | self.__children_and_ages[child.id()] = 1 | ||
26 | self.__children_and_wishes[child.id()] = wish | 26 | self.__children_and_wishes[child.id()] = wish | ||
27 | 27 | ||||
28 | def __matmul__(self, letter): | 28 | def __matmul__(self, letter): | ||
29 | child_id = re.search('(\s*)([0-9])+(\s*)', letter) | 29 | child_id = re.search('(\s*)([0-9])+(\s*)', letter) | ||
n | 30 | wish = re.search('[\'\"]([\w\t])+[\'\"]', letter) | n | 30 | wish = re.search(r'[\'\"]([\w\t])+[\'\"]', letter) |
31 | self.__wish_list.append(wish) | 31 | self.__wish_list.append(wish) | ||
32 | if child_id not in self.__children: | 32 | if child_id not in self.__children: | ||
33 | self.__children.append(child_id) | 33 | self.__children.append(child_id) | ||
34 | self.__children_and_ages[child_id] = 1 | 34 | self.__children_and_ages[child_id] = 1 | ||
35 | self.__children_and_wishes[child_id] = wish | 35 | self.__children_and_wishes[child_id] = wish | ||
36 | 36 | ||||
37 | def __iter__(self): | 37 | def __iter__(self): | ||
38 | self.num = 0 | 38 | self.num = 0 | ||
39 | return self | 39 | return self | ||
40 | 40 | ||||
41 | def __next__(self): | 41 | def __next__(self): | ||
42 | if self.num < len(self.__wish_list): | 42 | if self.num < len(self.__wish_list): | ||
43 | result = self.__wish_list[self.num] | 43 | result = self.__wish_list[self.num] | ||
44 | self.num += 1 | 44 | self.num += 1 | ||
45 | return result | 45 | return result | ||
46 | else: | 46 | else: | ||
47 | raise StopIteration | 47 | raise StopIteration | ||
48 | 48 | ||||
49 | def __get_most_desired(self): | 49 | def __get_most_desired(self): | ||
50 | count = {} | 50 | count = {} | ||
51 | for wish in self.__wish_list: | 51 | for wish in self.__wish_list: | ||
52 | count[wish] = self.__wish_list.count(wish) | 52 | count[wish] = self.__wish_list.count(wish) | ||
53 | __most_desired = max(count, key=count.get) | 53 | __most_desired = max(count, key=count.get) | ||
54 | self.__wish_list = [] | 54 | self.__wish_list = [] | ||
55 | self.__children = [] | 55 | self.__children = [] | ||
56 | self.__children_and_wishes = {} | 56 | self.__children_and_wishes = {} | ||
57 | 57 | ||||
58 | def xmas(self): | 58 | def xmas(self): | ||
59 | for child in self.__children: | 59 | for child in self.__children: | ||
60 | if self.__children_and_ages[child] <= 5: | 60 | if self.__children_and_ages[child] <= 5: | ||
61 | if child not in self.__children_and_wishes: | 61 | if child not in self.__children_and_wishes: | ||
62 | kid(self.__most_desired) | 62 | kid(self.__most_desired) | ||
63 | elif (): | 63 | elif (): | ||
64 | kid(self.__children_and_wishes[child]) | 64 | kid(self.__children_and_wishes[child]) | ||
65 | else: | 65 | else: | ||
66 | kid('coal') | 66 | kid('coal') | ||
67 | self.__children_and_ages[child] += 1 | 67 | self.__children_and_ages[child] += 1 | ||
68 | self.__get_most_desired() | 68 | self.__get_most_desired() | ||
69 | 69 | ||||
70 | 70 | ||||
t | t | 71 | class Kid: | ||
72 | pass | ||||
71 | 73 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
19.12.2024 16:46
19.12.2024 16:47
19.12.2024 16:48
19.12.2024 16:49
19.12.2024 16:45