Предизвикателства > Мемник > Решения > Решението на Панайот Пощов

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

0 точки общо

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

 1#Извинете за прекалените коментари, но така се надявам по-лесно да науча принципите
 2
 3memory = {}  #store in dict what_is_saids and the person being addressed
 4
 5def memnick(*args):  #function to receive positional arguments
 6    if not args:  #if no arguments, return a decorator
 7        def decorator(func):  #decorator, receive the function func
 8            def wrapper(*func_args, **func_kwargs):  #wrapper, replace the original function
 9                result = func(*func_args, **func_kwargs)  #call func, store its output
10                
11                who_is_addressed, what_is_said = result.split(", ", 1)  #split the returned string
12                
13                speaker = func.__who_is_addressed__.replace("_", " ").title()  #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
14                
15                memory.setdefault(who_is_addressed, set()).add((speaker, result)) #if who_is_addressed is not in memory, create a new empty set; then add a tuple (speaker, result) to that set, using a set ensures duplicate what_is_saids are not stored
16                
17                return result  #return the original result so the function behaves normally
18            
19            return wrapper  #return the wrapper that will replace the decorated function
20        
21        return decorator  #return the decorator when memnick() is used
22    
23    result = []  #list that will store the what_is_saids to return when querying memnick
24
25    for func in args:  #iterate through the functions passed to memnick (e.g. memnick(емил, божана))
26        who_is_addressed = func.__who_is_addressed__.replace("_", " ").title() #convert the function who_is_addressed to a readable person who_is_addressed (same formatting as before)
27        
28        if who_is_addressed in memory:  # check if someone has spoken to this person
29            for speaker, what_is_said in memory[who_is_addressed]:  #iterate through all stored what_is_saids addressed to them
30                result.append(f"С гласа на {speaker}: {what_is_said}")  #format the what_is_said like: "С гласа на Тодор: Иван, айде на риба!"
31    
32    return result  # return the final list of what_is_saids

EEEEE
======================================================================
ERROR: test_memnick_decorates_variable_functions (test.TestMemnick.test_memnick_decorates_variable_functions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 108, in test_memnick_decorates_variable_functions
solution.memnick()(граматиков)("Капан 1", "Капан 2", "кос", "друг кос", утрепан_от_мамника=True)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 13, in wrapper
speaker = func.__who_is_addressed__.replace("_", " ").title() #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__who_is_addressed__'

======================================================================
ERROR: test_memnick_does_not_memorize_repetitions (test.TestMemnick.test_memnick_does_not_memorize_repetitions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 70, in test_memnick_does_not_memorize_repetitions
solution.memnick()(бай_венци)()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/tmp/solution.py", line 13, in wrapper
speaker = func.__who_is_addressed__.replace("_", " ").title() #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__who_is_addressed__'

======================================================================
ERROR: test_memnick_memorizes (test.TestMemnick.test_memnick_memorizes)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 44, in test_memnick_memorizes
solution.memnick()(бай_венци)()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/tmp/solution.py", line 13, in wrapper
speaker = func.__who_is_addressed__.replace("_", " ").title() #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__who_is_addressed__'

======================================================================
ERROR: test_memnick_preserves_result (test.TestMemnick.test_memnick_preserves_result)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 82, in test_memnick_preserves_result
self.assertEqual(божана(), solution.memnick()(божана)())
~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/tmp/solution.py", line 13, in wrapper
speaker = func.__who_is_addressed__.replace("_", " ").title() #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__who_is_addressed__'

======================================================================
ERROR: test_memnick_splits_properly (test.TestMemnick.test_memnick_splits_properly)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 85, in test_memnick_splits_properly
solution.memnick()(бай_венци)()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/tmp/solution.py", line 13, in wrapper
speaker = func.__who_is_addressed__.replace("_", " ").title() #get the function who_is_addressed (the speaker), replace "_" with space, capitalize
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__who_is_addressed__'

----------------------------------------------------------------------
Ran 5 tests in 0.003s

FAILED (errors=5)

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