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

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

2 точки общо

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

 1import re
 2import random
 3
 4all_kids = {}
 5
 6def new_init_decorator(old_init):
 7    def args_wrapper(self, *args, **kwargs):
 8        if old_init:
 9            old_init(self, *args, **kwargs)
10        
11        self.error_detected = False
12        self.xmas_count = 0
13        all_kids[id(self)] = self
14    return args_wrapper
15
16class Kid(type):
17    def __new__(cls, name, bases, dct):
18        if '__call__' not in dct:
19            raise NotImplementedError("Този път се надявам на половината точки.")
20        
21        old_init = dct.get("__init__", None)
22        dct["__init__"] = new_init_decorator(old_init)
23 
24        return super().__new__(cls, name, bases, dct)
25    
26
27class Santa:
28    _instance = None
29
30    def __new__(cls):
31        if cls._instance is None:
32            cls._instance = super(Santa, cls).__new__(cls)
33        return cls._instance
34
35    def __init__(self):
36        self.gifts= {}
37        self._index = 0 
38
39    def __iter__(self):
40        self._index = 0
41        return self
42
43    def __next__(self):
44        if self._index < len(self.gifts):
45            gift = self.gifts[self._index]
46            self._index += 1
47            return gift
48        raise StopIteration
49    
50    @staticmethod
51    def search_for_gift(text):
52        wish_match = re.search(r'(["\'])([a-zA-Z0-9 ]+)(\1)', text)
53        if wish_match:
54            return wish_match.group(2)
55        return None
56    
57    def __call__(self, child, wish):
58        child_id = id(child)
59        gift = self.search_for_gift(wish)
60        if gift:
61            self.gifts[child_id] = gift    
62
63    @staticmethod
64    def search_for_signature(text):
65        signature_match = re.search(r'^\s*(\d+)\s*$', text, re.MULTILINE)
66        if signature_match:
67            signature = signature_match.group(1)
68            return int(signature)
69        return None
70
71    def __matmul__(self, letter):
72        child_id = self.search_for_signature(letter)
73        if child_id:
74            gift = self.search_for_gift(letter)
75            if gift:
76                self.gifts[child_id] = gift  

..EEEEEEE.EEEEEEEEEE
======================================================================
ERROR: test_call (test.TestSanta.test_call)
Test sending message via calling.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 56, in test_call
self.assertEqual(list(self.santa), ['toy1', 'toy2'])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
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 71, in test_call_and_mail_same_kid
self.assertEqual(list(self.santa), ['toy1'])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
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 129, in test_iterable
self.assertEqual(list(self.santa), ['something', 'something else'])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
ERROR: 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'])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
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 92, in test_present_matching
self.assertEqual(list(self.santa), ['toy4', 'abcdefgHIJKLMNopQRstUVwxYZ 1 2 3 4567890 '])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
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 260, in test_santa_gift_order
self.assertEqual(list(self.santa), ["toy2v2", "toy3", "toy1"])
^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 45, in __next__
gift = self.gifts[self._index]
~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 0

======================================================================
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 117, in test_signature_matching
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
ERROR: test_xmass (test.TestSanta.test_xmass)
Test a simple Christmas case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 140, in test_xmass
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 186, in test_xmass_kid_with_multiple_wishes
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 174, in test_xmass_kid_without_a_wish
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 226, in test_xmass_naughty
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 150, in test_xmass_no_wishes
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 162, in test_xmass_no_wishes_but_naughty_kids
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 239, in test_xmass_private_with_error
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 247, in test_xmass_public_with_no_error
self.santa.xmas()
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 206, in test_xmass_years_5_and_over
self.santa.xmas() # Christmas 1
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

======================================================================
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 193, in test_xmass_years_under_5
self.santa.xmas() # Christmas 1
^^^^^^^^^^^^^^^
AttributeError: 'Santa' object has no attribute 'xmas'

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

FAILED (errors=17)

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

f1import ref1import re
2import random2import random
nn3 
4all_kids = {}
5 
6def new_init_decorator(old_init):
7    def args_wrapper(self, *args, **kwargs):
8        if old_init:
9            old_init(self, *args, **kwargs)
10        
11        self.error_detected = False
12        self.xmas_count = 0
13        all_kids[id(self)] = self
14    return args_wrapper
315
4class Kid(type):16class Kid(type):
5    def __new__(cls, name, bases, dct):17    def __new__(cls, name, bases, dct):
6        if '__call__' not in dct:18        if '__call__' not in dct:
n7            raise NotImplementedError("Този път се надяваме на половината точки.")n19            raise NotImplementedError("Този път се надявам на половината точки.")
20        
21        old_init = dct.get("__init__", None)
22        dct["__init__"] = new_init_decorator(old_init)
23 
8        return super().__new__(cls, name, bases, dct)24        return super().__new__(cls, name, bases, dct)
n9 n25    
10class Child:
11    def __init__(self):
12        self.id = id(self)
1326
14class Santa:27class Santa:
15    _instance = None28    _instance = None
1629
17    def __new__(cls):30    def __new__(cls):
18        if cls._instance is None:31        if cls._instance is None:
19            cls._instance = super(Santa, cls).__new__(cls)32            cls._instance = super(Santa, cls).__new__(cls)
20        return cls._instance33        return cls._instance
2134
22    def __init__(self):35    def __init__(self):
n23        self.gifts= []n36        self.gifts= {}
24        self._index = 0 37        self._index = 0 
2538
26    def __iter__(self):39    def __iter__(self):
27        self._index = 040        self._index = 0
28        return self41        return self
2942
30    def __next__(self):43    def __next__(self):
31        if self._index < len(self.gifts):44        if self._index < len(self.gifts):
32            gift = self.gifts[self._index]45            gift = self.gifts[self._index]
33            self._index += 146            self._index += 1
34            return gift47            return gift
35        raise StopIteration48        raise StopIteration
36    49    
37    @staticmethod50    @staticmethod
38    def search_for_gift(text):51    def search_for_gift(text):
39        wish_match = re.search(r'(["\'])([a-zA-Z0-9 ]+)(\1)', text)52        wish_match = re.search(r'(["\'])([a-zA-Z0-9 ]+)(\1)', text)
40        if wish_match:53        if wish_match:
41            return wish_match.group(2)54            return wish_match.group(2)
42        return None55        return None
43    56    
44    def __call__(self, child, wish):57    def __call__(self, child, wish):
45        child_id = id(child)58        child_id = id(child)
46        gift = self.search_for_gift(wish)59        gift = self.search_for_gift(wish)
47        if gift:60        if gift:
n48            self.gifts.append(gift)    n61            self.gifts[child_id] = gift    
4962
50    @staticmethod63    @staticmethod
51    def search_for_signature(text):64    def search_for_signature(text):
52        signature_match = re.search(r'^\s*(\d+)\s*$', text, re.MULTILINE)65        signature_match = re.search(r'^\s*(\d+)\s*$', text, re.MULTILINE)
53        if signature_match:66        if signature_match:
54            signature = signature_match.group(1)67            signature = signature_match.group(1)
55            return int(signature)68            return int(signature)
56        return None69        return None
5770
58    def __matmul__(self, letter):71    def __matmul__(self, letter):
59        child_id = self.search_for_signature(letter)72        child_id = self.search_for_signature(letter)
60        if child_id:73        if child_id:
61            gift = self.search_for_gift(letter)74            gift = self.search_for_gift(letter)
62            if gift:75            if gift:
t63                self.gifts.append(gift)t76                self.gifts[child_id] = gift  
64 
65 
66 
67 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op