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

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

5 точки общо

9 успешни теста
11 неуспешни теста
Код
Скрий всички коментари

 1import re
 2from collections import Counter
 3
 4class Kid(type):
 5    instances = {}
 6    def __new__(cls, name, bases, attr_dict):
 7        if "__call__" not in attr_dict.keys():
 8            raise NotImplementedError
 9
10        def meta_init(self, *args, **kwargs):
11            cls.instances[id(self)] = (self, 0)
12        attr_dict['__init__'] = meta_init
13
14        def meta_call(self, gift):
15            pass
16        attr_dict['__call__'] = meta_call
17
18        result = type.__new__(cls, name, bases, attr_dict)
19        return result
20
21
22class Santa:
23    instance = None
24    presents_for_kids = {}
25    self_iter = 0
26
27    def __new__(cls):
28        if cls.instance is None:
29            cls.instance = super().__new__(cls)
30        return cls.instance
31
32    def __call__(self, kid, wish):
33        regex_wish = r'(\'|\")([\w, ' ']+)+(\'|\")'
34        present = re.findall(regex_wish, wish, re.A)[0][1]
35
36        self.presents_for_kids[id(kid)] = present
37
38    def __matmul__(self, letter):
39        regex_id = r'\n\s*\d+\s*'
40        regex_wish = r'(\'|\")([\w, ' ']+)+(\'|\")'
41        kid_id = re.findall(regex_id, letter)
42        kid_id = re.findall(r'\d+', kid_id[0])
43        kid_id = int(kid_id[0])
44        present = re.findall(regex_wish, letter, re.A)[0][1]
45        self.presents_for_kids[kid_id] = present
46
47    def __iter__(self):
48        self.self_iter = self.presents_for_kids.values().__iter__()
49        return self.self_iter
50
51    def __next__(self):
52        return self.self_iter.__next__()
53
54    def xmas(self):
55        if len(self.presents_for_kids) == 0:
56            return
57
58        for kid_id in self.presents_for_kids.keys():
59            if Kid.instances[kid_id][1] < 5:
60                Kid.instances[kid_id] = (Kid.instances[kid_id][0], Kid.instances[kid_id][1] + 1)
61                Kid.instances[kid_id][0](self.presents_for_kids[kid_id])
62        most_wanted_gift = Counter(list(self.presents_for_kids.values())).most_common()
63        for id, kid in Kid.instances.items():
64            if kid[1] >= 5:
65                continue
66            if id not in self.presents_for_kids.keys():
67                kid[0](most_wanted_gift[0][0])
68
69        self.presents_for_kids = {}

........E.EEEEEEEEEE
======================================================================
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 118, in test_signature_matching
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
ERROR: 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')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 187, in test_xmass_kid_with_multiple_wishes
self.assertEqual(kid1.SECRET_PRESENT, 'toy3')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 175, in test_xmass_kid_without_a_wish
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 227, in test_xmass_naughty
self.assertEqual(kid1.SECRET_PRESENT, 'coal')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 151, in test_xmass_no_wishes
self.assertEqual(kid1.SECRET_PRESENT, None)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 163, in test_xmass_no_wishes_but_naughty_kids
self.assertEqual(kid1.SECRET_PRESENT, None)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 240, in test_xmass_private_with_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 248, in test_xmass_public_with_no_error
self.assertEqual(kid1.SECRET_PRESENT, 'sirenie')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 213, in test_xmass_years_5_and_over
self.assertEqual(kid1.SECRET_PRESENT, None)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

======================================================================
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 194, in test_xmass_years_under_5
self.assertEqual(kid1.SECRET_PRESENT, 'toy1')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'KidClass1' object has no attribute 'SECRET_PRESENT'

----------------------------------------------------------------------
Ran 20 tests in 0.027s

FAILED (errors=11)

Дискусия
История
Това решение има само една версия.