Предизвикателства > Сляпа баба > Решения > Решението на Владимир Великов

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

1 точки общо

0 успешни теста
1 неуспешни теста
Код

 1import secret
 2from random import choices
 3from inspect import getsource
 4
 5fn = 'FN0MI0600204'
 6type_error_message = "Опаааааа, тука има нещо нередно."
 7
 8def raises_error(instance, func):
 9    try:
10        func(instance)
11    except TypeError as type_error:
12        return True if str(type_error) == type_error_message else False
13    except BaseException as base_exception:
14        rep = repr(base_exception)
15        return True if type(rep) == str else False
16    except:
17        return False   
18    return False
19    
20def is_squarer(instance, func):
21    try:
22        nums = [x for x in range(1, 11)]
23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]
24    except:
25        return False
26
27def is_concat(instance, func):
28    try:
29        str_len = 5
30        left_str = ''.join(choices(fn, k=str_len))
31        right_str = ''.join(choices(fn, k=str_len))
32        left_first = left_str + right_str
33        right_first = right_str + left_str
34            
35        func_res = func(instance, left=left_str, right=right_str)
36        func_res_reversed = func(instance, left=right_str, right=left_str)
37        return left_first == func_res and func_res_reversed == right_first
38    except:
39        return False
40
41def is_static(func):
42    try:
43        code = getsource(func)
44        return 'self' not in code and 'cls' not in code
45    except:
46        return False
47
48def is_special(caller, attr_name):
49    attr = getattr(caller, attr_name)
50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))
51
52def methodify():
53    def methodify_util(mod, attr, func_names):
54        current = getattr(mod, attr)
55        attrs = [item for item in dir(current) if item.startswith('__') is False]
56        for item in attrs:
57            if 'clue' in item.lower():
58                methodify_util(current, item, func_names)
59            
60            current_attr = getattr(current, item)
61            if len(current_attr.__name__) == 1 and is_special(current, item):
62                    func_names.append(current_attr.__name__)
63                
64    func_names = []
65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]
66    for attr in attrs:
67        if 'clue' in attr.lower():
68            methodify_util(secret, attr, func_names)
69    
70    faculty_num = []           
71    for letter in fn:
72        for func in func_names:
73            if letter == func:
74                faculty_num.append(func)
75                func_names.remove(func)
76                
77    return tuple(faculty_num)

E
======================================================================
ERROR: test_metodify (test.TestMethodify)
Test metodify function.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 12, in test_metodify
self.assertIn(methodify(), _RESULTS.keys())
File "/tmp/solution.py", line 68, in methodify
methodify_util(secret, attr, func_names)
File "/tmp/solution.py", line 58, in methodify_util
methodify_util(current, item, func_names)
File "/tmp/solution.py", line 61, in methodify_util
if len(current_attr.__name__) == 1 and is_special(current, item):
AttributeError: 'tuple' object has no attribute '__name__'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

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

f1import secretf1import secret
2from random import choices2from random import choices
3from inspect import getsource3from inspect import getsource
44
5fn = 'FN0MI0600204'5fn = 'FN0MI0600204'
6type_error_message = "Опаааааа, тука има нещо нередно."6type_error_message = "Опаааааа, тука има нещо нередно."
77
8def raises_error(instance, func):8def raises_error(instance, func):
9    try:9    try:
10        func(instance)10        func(instance)
11    except TypeError as type_error:11    except TypeError as type_error:
12        return True if str(type_error) == type_error_message else False12        return True if str(type_error) == type_error_message else False
13    except BaseException as base_exception:13    except BaseException as base_exception:
14        rep = repr(base_exception)14        rep = repr(base_exception)
15        return True if type(rep) == str else False15        return True if type(rep) == str else False
16    except:16    except:
17        return False   17        return False   
18    return False18    return False
19    19    
20def is_squarer(instance, func):20def is_squarer(instance, func):
21    try:21    try:
22        nums = [x for x in range(1, 11)]22        nums = [x for x in range(1, 11)]
23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]
t24    except Exception as err:t24    except:
25        return False25        return False
2626
27def is_concat(instance, func):27def is_concat(instance, func):
28    try:28    try:
29        str_len = 529        str_len = 5
30        left_str = ''.join(choices(fn, k=str_len))30        left_str = ''.join(choices(fn, k=str_len))
31        right_str = ''.join(choices(fn, k=str_len))31        right_str = ''.join(choices(fn, k=str_len))
32        left_first = left_str + right_str32        left_first = left_str + right_str
33        right_first = right_str + left_str33        right_first = right_str + left_str
34            34            
35        func_res = func(instance, left=left_str, right=right_str)35        func_res = func(instance, left=left_str, right=right_str)
36        func_res_reversed = func(instance, left=right_str, right=left_str)36        func_res_reversed = func(instance, left=right_str, right=left_str)
37        return left_first == func_res and func_res_reversed == right_first37        return left_first == func_res and func_res_reversed == right_first
38    except:38    except:
39        return False39        return False
4040
41def is_static(func):41def is_static(func):
42    try:42    try:
43        code = getsource(func)43        code = getsource(func)
44        return 'self' not in code and 'cls' not in code44        return 'self' not in code and 'cls' not in code
45    except:45    except:
46        return False46        return False
4747
48def is_special(caller, attr_name):48def is_special(caller, attr_name):
49    attr = getattr(caller, attr_name)49    attr = getattr(caller, attr_name)
50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))
5151
52def methodify():52def methodify():
53    def methodify_util(mod, attr, func_names):53    def methodify_util(mod, attr, func_names):
54        current = getattr(mod, attr)54        current = getattr(mod, attr)
55        attrs = [item for item in dir(current) if item.startswith('__') is False]55        attrs = [item for item in dir(current) if item.startswith('__') is False]
56        for item in attrs:56        for item in attrs:
57            if 'clue' in item.lower():57            if 'clue' in item.lower():
58                methodify_util(current, item, func_names)58                methodify_util(current, item, func_names)
59            59            
60            current_attr = getattr(current, item)60            current_attr = getattr(current, item)
61            if len(current_attr.__name__) == 1 and is_special(current, item):61            if len(current_attr.__name__) == 1 and is_special(current, item):
62                    func_names.append(current_attr.__name__)62                    func_names.append(current_attr.__name__)
63                63                
64    func_names = []64    func_names = []
65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]
66    for attr in attrs:66    for attr in attrs:
67        if 'clue' in attr.lower():67        if 'clue' in attr.lower():
68            methodify_util(secret, attr, func_names)68            methodify_util(secret, attr, func_names)
69    69    
70    faculty_num = []           70    faculty_num = []           
71    for letter in fn:71    for letter in fn:
72        for func in func_names:72        for func in func_names:
73            if letter == func:73            if letter == func:
74                faculty_num.append(func)74                faculty_num.append(func)
75                func_names.remove(func)75                func_names.remove(func)
76                76                
77    return tuple(faculty_num)77    return tuple(faculty_num)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import secretf1import secret
2from random import choices2from random import choices
3from inspect import getsource3from inspect import getsource
44
5fn = 'FN0MI0600204'5fn = 'FN0MI0600204'
6type_error_message = "Опаааааа, тука има нещо нередно."6type_error_message = "Опаааааа, тука има нещо нередно."
77
8def raises_error(instance, func):8def raises_error(instance, func):
9    try:9    try:
10        func(instance)10        func(instance)
11    except TypeError as type_error:11    except TypeError as type_error:
12        return True if str(type_error) == type_error_message else False12        return True if str(type_error) == type_error_message else False
13    except BaseException as base_exception:13    except BaseException as base_exception:
14        rep = repr(base_exception)14        rep = repr(base_exception)
15        return True if type(rep) == str else False15        return True if type(rep) == str else False
16    except:16    except:
17        return False   17        return False   
18    return False18    return False
19    19    
20def is_squarer(instance, func):20def is_squarer(instance, func):
21    try:21    try:
22        nums = [x for x in range(1, 11)]22        nums = [x for x in range(1, 11)]
23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]
24    except Exception as err:24    except Exception as err:
t25        print(err)t25        return False
2626
27def is_concat(instance, func):27def is_concat(instance, func):
28    try:28    try:
29        str_len = 529        str_len = 5
30        left_str = ''.join(choices(fn, k=str_len))30        left_str = ''.join(choices(fn, k=str_len))
31        right_str = ''.join(choices(fn, k=str_len))31        right_str = ''.join(choices(fn, k=str_len))
32        left_first = left_str + right_str32        left_first = left_str + right_str
33        right_first = right_str + left_str33        right_first = right_str + left_str
34            34            
35        func_res = func(instance, left=left_str, right=right_str)35        func_res = func(instance, left=left_str, right=right_str)
36        func_res_reversed = func(instance, left=right_str, right=left_str)36        func_res_reversed = func(instance, left=right_str, right=left_str)
37        return left_first == func_res and func_res_reversed == right_first37        return left_first == func_res and func_res_reversed == right_first
38    except:38    except:
39        return False39        return False
4040
41def is_static(func):41def is_static(func):
42    try:42    try:
43        code = getsource(func)43        code = getsource(func)
44        return 'self' not in code and 'cls' not in code44        return 'self' not in code and 'cls' not in code
45    except:45    except:
46        return False46        return False
4747
48def is_special(caller, attr_name):48def is_special(caller, attr_name):
49    attr = getattr(caller, attr_name)49    attr = getattr(caller, attr_name)
50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))
5151
52def methodify():52def methodify():
53    def methodify_util(mod, attr, func_names):53    def methodify_util(mod, attr, func_names):
54        current = getattr(mod, attr)54        current = getattr(mod, attr)
55        attrs = [item for item in dir(current) if item.startswith('__') is False]55        attrs = [item for item in dir(current) if item.startswith('__') is False]
56        for item in attrs:56        for item in attrs:
57            if 'clue' in item.lower():57            if 'clue' in item.lower():
58                methodify_util(current, item, func_names)58                methodify_util(current, item, func_names)
59            59            
60            current_attr = getattr(current, item)60            current_attr = getattr(current, item)
61            if len(current_attr.__name__) == 1 and is_special(current, item):61            if len(current_attr.__name__) == 1 and is_special(current, item):
62                    func_names.append(current_attr.__name__)62                    func_names.append(current_attr.__name__)
63                63                
64    func_names = []64    func_names = []
65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]
66    for attr in attrs:66    for attr in attrs:
67        if 'clue' in attr.lower():67        if 'clue' in attr.lower():
68            methodify_util(secret, attr, func_names)68            methodify_util(secret, attr, func_names)
69    69    
70    faculty_num = []           70    faculty_num = []           
71    for letter in fn:71    for letter in fn:
72        for func in func_names:72        for func in func_names:
73            if letter == func:73            if letter == func:
74                faculty_num.append(func)74                faculty_num.append(func)
75                func_names.remove(func)75                func_names.remove(func)
76                76                
77    return tuple(faculty_num)77    return tuple(faculty_num)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import secretf1import secret
2from random import choices2from random import choices
3from inspect import getsource3from inspect import getsource
44
5fn = 'FN0MI0600204'5fn = 'FN0MI0600204'
6type_error_message = "Опаааааа, тука има нещо нередно."6type_error_message = "Опаааааа, тука има нещо нередно."
77
8def raises_error(instance, func):8def raises_error(instance, func):
9    try:9    try:
10        func(instance)10        func(instance)
11    except TypeError as type_error:11    except TypeError as type_error:
12        return True if str(type_error) == type_error_message else False12        return True if str(type_error) == type_error_message else False
13    except BaseException as base_exception:13    except BaseException as base_exception:
14        rep = repr(base_exception)14        rep = repr(base_exception)
15        return True if type(rep) == str else False15        return True if type(rep) == str else False
16    except:16    except:
17        return False   17        return False   
18    return False18    return False
19    19    
20def is_squarer(instance, func):20def is_squarer(instance, func):
21    try:21    try:
22        nums = [x for x in range(1, 11)]22        nums = [x for x in range(1, 11)]
23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]
24    except Exception as err:24    except Exception as err:
25        print(err)25        print(err)
2626
27def is_concat(instance, func):27def is_concat(instance, func):
28    try:28    try:
29        str_len = 529        str_len = 5
30        left_str = ''.join(choices(fn, k=str_len))30        left_str = ''.join(choices(fn, k=str_len))
31        right_str = ''.join(choices(fn, k=str_len))31        right_str = ''.join(choices(fn, k=str_len))
32        left_first = left_str + right_str32        left_first = left_str + right_str
33        right_first = right_str + left_str33        right_first = right_str + left_str
34            34            
35        func_res = func(instance, left=left_str, right=right_str)35        func_res = func(instance, left=left_str, right=right_str)
36        func_res_reversed = func(instance, left=right_str, right=left_str)36        func_res_reversed = func(instance, left=right_str, right=left_str)
37        return left_first == func_res and func_res_reversed == right_first37        return left_first == func_res and func_res_reversed == right_first
38    except:38    except:
39        return False39        return False
4040
41def is_static(func):41def is_static(func):
42    try:42    try:
43        code = getsource(func)43        code = getsource(func)
44        return 'self' not in code and 'cls' not in code44        return 'self' not in code and 'cls' not in code
45    except:45    except:
46        return False46        return False
4747
48def is_special(caller, attr_name):48def is_special(caller, attr_name):
49    attr = getattr(caller, attr_name)49    attr = getattr(caller, attr_name)
50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))
5151
n52def metodify():n52def methodify():
53    def metodify_util(mod, attr, func_names):53    def methodify_util(mod, attr, func_names):
54        current = getattr(mod, attr)54        current = getattr(mod, attr)
55        attrs = [item for item in dir(current) if item.startswith('__') is False]55        attrs = [item for item in dir(current) if item.startswith('__') is False]
56        for item in attrs:56        for item in attrs:
57            if 'clue' in item.lower():57            if 'clue' in item.lower():
n58                metodify_util(current, item, func_names)n58                methodify_util(current, item, func_names)
59            59            
60            current_attr = getattr(current, item)60            current_attr = getattr(current, item)
61            if len(current_attr.__name__) == 1 and is_special(current, item):61            if len(current_attr.__name__) == 1 and is_special(current, item):
62                    func_names.append(current_attr.__name__)62                    func_names.append(current_attr.__name__)
63                63                
64    func_names = []64    func_names = []
65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]
66    for attr in attrs:66    for attr in attrs:
67        if 'clue' in attr.lower():67        if 'clue' in attr.lower():
t68            metodify_util(secret, attr, func_names)t68            methodify_util(secret, attr, func_names)
69    69    
70    faculty_num = []           70    faculty_num = []           
71    for letter in fn:71    for letter in fn:
72        for func in func_names:72        for func in func_names:
73            if letter == func:73            if letter == func:
74                faculty_num.append(func)74                faculty_num.append(func)
75                func_names.remove(func)75                func_names.remove(func)
76                76                
77    return tuple(faculty_num)77    return tuple(faculty_num)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import secretf1import secret
2from random import choices2from random import choices
3from inspect import getsource3from inspect import getsource
44
5fn = 'FN0MI0600204'5fn = 'FN0MI0600204'
6type_error_message = "Опаааааа, тука има нещо нередно."6type_error_message = "Опаааааа, тука има нещо нередно."
77
8def raises_error(instance, func):8def raises_error(instance, func):
9    try:9    try:
10        func(instance)10        func(instance)
11    except TypeError as type_error:11    except TypeError as type_error:
12        return True if str(type_error) == type_error_message else False12        return True if str(type_error) == type_error_message else False
13    except BaseException as base_exception:13    except BaseException as base_exception:
14        rep = repr(base_exception)14        rep = repr(base_exception)
15        return True if type(rep) == str else False15        return True if type(rep) == str else False
16    except:16    except:
17        return False   17        return False   
18    return False18    return False
19    19    
20def is_squarer(instance, func):20def is_squarer(instance, func):
21    try:21    try:
22        nums = [x for x in range(1, 11)]22        nums = [x for x in range(1, 11)]
23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]23        return [x ** 2 if x % 2 == 0 else 0 for x in nums] == [func(instance, x) for x in nums]
24    except Exception as err:24    except Exception as err:
25        print(err)25        print(err)
2626
27def is_concat(instance, func):27def is_concat(instance, func):
28    try:28    try:
29        str_len = 529        str_len = 5
30        left_str = ''.join(choices(fn, k=str_len))30        left_str = ''.join(choices(fn, k=str_len))
31        right_str = ''.join(choices(fn, k=str_len))31        right_str = ''.join(choices(fn, k=str_len))
32        left_first = left_str + right_str32        left_first = left_str + right_str
33        right_first = right_str + left_str33        right_first = right_str + left_str
34            34            
35        func_res = func(instance, left=left_str, right=right_str)35        func_res = func(instance, left=left_str, right=right_str)
36        func_res_reversed = func(instance, left=right_str, right=left_str)36        func_res_reversed = func(instance, left=right_str, right=left_str)
37        return left_first == func_res and func_res_reversed == right_first37        return left_first == func_res and func_res_reversed == right_first
38    except:38    except:
39        return False39        return False
4040
41def is_static(func):41def is_static(func):
42    try:42    try:
43        code = getsource(func)43        code = getsource(func)
44        return 'self' not in code and 'cls' not in code44        return 'self' not in code and 'cls' not in code
45    except:45    except:
46        return False46        return False
4747
48def is_special(caller, attr_name):48def is_special(caller, attr_name):
49    attr = getattr(caller, attr_name)49    attr = getattr(caller, attr_name)
50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))50    return (raises_error(caller, attr) or is_squarer(caller, attr) or is_concat(caller, attr) or is_static(attr))
5151
52def metodify():52def metodify():
53    def metodify_util(mod, attr, func_names):53    def metodify_util(mod, attr, func_names):
54        current = getattr(mod, attr)54        current = getattr(mod, attr)
55        attrs = [item for item in dir(current) if item.startswith('__') is False]55        attrs = [item for item in dir(current) if item.startswith('__') is False]
56        for item in attrs:56        for item in attrs:
57            if 'clue' in item.lower():57            if 'clue' in item.lower():
58                metodify_util(current, item, func_names)58                metodify_util(current, item, func_names)
59            59            
60            current_attr = getattr(current, item)60            current_attr = getattr(current, item)
61            if len(current_attr.__name__) == 1 and is_special(current, item):61            if len(current_attr.__name__) == 1 and is_special(current, item):
62                    func_names.append(current_attr.__name__)62                    func_names.append(current_attr.__name__)
63                63                
64    func_names = []64    func_names = []
65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]65    attrs = [attr for attr in dir(secret) if attr.startswith('__') is False]
66    for attr in attrs:66    for attr in attrs:
67        if 'clue' in attr.lower():67        if 'clue' in attr.lower():
68            metodify_util(secret, attr, func_names)68            metodify_util(secret, attr, func_names)
69    69    
70    faculty_num = []           70    faculty_num = []           
71    for letter in fn:71    for letter in fn:
72        for func in func_names:72        for func in func_names:
73            if letter == func:73            if letter == func:
74                faculty_num.append(func)74                faculty_num.append(func)
75                func_names.remove(func)75                func_names.remove(func)
76                76                
77    return tuple(faculty_num)77    return tuple(faculty_num)
t78        t
79print(metodify())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op