1import re
2
3class Santa:
4 _instance = None
5
6 def __new__(cls):
7 if cls._instance is None:
8 cls._instance = super().__new__(cls)
9 cls._instance._requests = []
10 cls._instance._children = {}
11 cls._instance._last_xmas_requests = []
12 cls._instance._child_xmas_count = {}
13 return cls._instance
14
15 def __call__(self, child, wish):
16 gift = self._extract_gift(wish)
17 if gift:
18 self._requests.append((child, gift))
19 self._last_xmas_requests.append((child, gift))
20
21 def __matmul__(self, letter):
22 child_id, gift = self._parse_letter(letter)
23 if child_id is not None and gift:
24 child = self._children.get(child_id)
25 if child:
26 self._requests.append((child, gift))
27 self._last_xmas_requests.append((child, gift))
28
29 def __iter__(self):
30 return iter(gift for _, gift in self._requests)
31
32 def xmas(self):
33 delivered = set()
34 most_wanted = self._most_wanted_gift()
35
36 for child, gift in self._last_xmas_requests:
37 if child in delivered:
38 continue
39
40 if self._child_xmas_count.get(child, 0) >= 5:
41 continue
42
43 if self._is_naughty(child):
44 child("coal")
45 else:
46 child(gift)
47
48 delivered.add(child)
49
50 for child in self._children.values():
51 if child not in delivered and self._child_xmas_count.get(child, 0) < 5:
52 child(most_wanted if most_wanted else "coal")
53 delivered.add(child)
54
55 self._last_xmas_requests.clear()
56 for child in delivered:
57 self._child_xmas_count[child] = self._child_xmas_count.get(child, 0) + 1
58
59 def _extract_gift(self, wish):
60 match = re.search(r'["\']([^"\']+)["\']', wish)
61 return match.group(1) if match else None
62
63 def _parse_letter(self, letter):
64 child_id_match = re.search(r"^(\d+)$", letter, re.MULTILINE)
65 gift_match = re.search(r'"([^"]+)"|\'([^\']+)\'', letter)
66 child_id = int(child_id_match.group(1)) if child_id_match else None
67 gift = gift_match.group(1) if gift_match else None
68 return child_id, gift
69
70 def _is_naughty(self, child):
71 for attr in dir(child):
72 if not attr.startswith("_") and callable(getattr(child, attr)):
73 try:
74 getattr(child, attr)()
75 except Exception:
76 return True
77 return False
78
79 def _most_wanted_gift(self):
80 gift_count = {}
81 for _, gift in self._last_xmas_requests:
82 gift_count[gift] = gift_count.get(gift, 0) + 1
83
84 most_wanted = None
85 max_count = 0
86 for gift, count in gift_count.items():
87 if count > max_count:
88 most_wanted = gift
89 max_count = count
90
91 return most_wanted
92
93class Kid(type):
94 def __new__(cls, name, bases, dct):
95 if "__call__" not in dct:
96 raise NotImplementedError("Опитвате се да дефинирате клас, чиито инстанции не могат да се извикват.")
97 return super().__new__(cls, name, bases, dct)
....FFFFF.FFFF..FFFF
======================================================================
FAIL: 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 129, in test_iterable
self.assertEqual(list(self.santa), ['something', 'something else'])
AssertionError: Lists differ: ['something', 'something not used', 'something else'] != ['something', 'something else']
First differing element 1:
'something not used'
'something else'
First list contains 1 additional elements.
First extra element 2:
'something else'
- ['something', 'something not used', 'something else']
+ ['something', 'something else']
======================================================================
FAIL: test_mail (test.TestSanta.test_mail)
Test sending message via email.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 64, in test_mail
self.assertEqual(list(self.santa), ['toy1', 'toy2'])
AssertionError: Lists differ: [] != ['toy1', 'toy2']
Second list contains 2 additional elements.
First extra element 0:
'toy1'
- []
+ ['toy1', 'toy2']
======================================================================
FAIL: test_present_matching (test.TestSanta.test_present_matching)
Test matching signature in the letter.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 92, in test_present_matching
self.assertEqual(list(self.santa), ['toy4', 'abcdefgHIJKLMNopQRstUVwxYZ 1 2 3 4567890 '])
AssertionError: Lists differ: [] != ['toy4', 'abcdefgHIJKLMNopQRstUVwxYZ 1 2 3 4567890 ']
Second list contains 2 additional elements.
First extra element 0:
'toy4'
- []
+ ['toy4', 'abcdefgHIJKLMNopQRstUVwxYZ 1 2 3 4567890 ']
======================================================================
FAIL: 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 260, in test_santa_gift_order
self.assertEqual(list(self.santa), ["toy2v2", "toy3", "toy1"])
AssertionError: Lists differ: ['toy2', 'toy1', 'toy2v2'] != ['toy2v2', 'toy3', 'toy1']
First differing element 0:
'toy2'
'toy2v2'
- ['toy2', 'toy1', 'toy2v2']
+ ['toy2v2', 'toy3', '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 118, in test_signature_matching
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: None != 'toy1'
======================================================================
FAIL: test_xmass (test.TestSanta.test_xmass)
Test a simple Christmas case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 141, in test_xmass
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: 'coal' != 'toy1'
- coal
+ toy1
======================================================================
FAIL: 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 187, in test_xmass_kid_with_multiple_wishes
self.assertEqual(kid1.SECRET_PRESENT, 'toy3')
AssertionError: 'coal' != 'toy3'
- coal
+ 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 175, in test_xmass_kid_without_a_wish
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: 'coal' != 'toy1'
- coal
+ toy1
======================================================================
FAIL: test_xmass_naughty (test.TestSanta.test_xmass_naughty)
Test a Christmas with naughty kids.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 229, in test_xmass_naughty
self.assertEqual(kid3.SECRET_PRESENT, 'sirenie')
AssertionError: None != 'sirenie'
======================================================================
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 248, in test_xmass_public_with_no_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
AssertionError: 'coal' != 'sirenie'
- coal
+ sirenie
======================================================================
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: 'coal' != None
======================================================================
FAIL: 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 194, in test_xmass_years_under_5
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
AssertionError: 'coal' != 'toy1'
- coal
+ toy1
----------------------------------------------------------------------
Ran 20 tests in 0.057s
FAILED (failures=13)
Виктор Бечев
20.12.2024 11:42Като код нещата изглеждат окей. Вероятно има дребни неща, които не успявам да забележа, защото не съм безкрайно концентриран в момента, но проблемите в решението изглеждат единствено имплементационни (ерго и фейлващите тестове).
|
20.12.2024 11:34