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

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

1 точки общо

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

 1import re
 2
 3import secret
 4
 5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."
 6
 7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'
 8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'
 9
10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}
11
12
13def validate_interesting_method(current_function):
14    """Validate if the current function is the one needed"""
15    try:
16        method_check = current_function.__self__
17        if current_function.__code__.co_argcount == 2:
18            if current_function(2) == 4 and current_function(3) == 0:
19                return True
20        if current_function.__code__.co_argcount == 3:
21            if current_function('left ', 'right') == 'left right':
22                return True
23    except AttributeError as e:
24        if str(e) == "'function' object has no attribute '__self__'":
25            return True
26    except TypeError as e:
27        if str(e) == TYPE_ERROR_TEXT_MESSAGE:
28            return True
29    except BaseException as e:
30        if str(e):
31            return True
32
33    return False
34
35
36def recursive_methodify(curr_object, gathered_methods):
37    """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""
38    if needed_letters == {}:
39        return
40
41    curr_dict_keys = dir(curr_object)
42
43    for key in curr_dict_keys:
44        if needed_letters == {}:
45            break
46
47        current_function_or_object = getattr(curr_object, key)
48        if (
49                callable(current_function_or_object) and
50                re.match(METHOD_NAME_PATTERN, key) and
51                key in needed_letters and
52                validate_interesting_method(current_function_or_object)
53        ):
54            needed_letters.remove(key)
55            gathered_methods[key] = gathered_methods.get(key, current_function_or_object)
56        elif re.match(OBJECT_NAME_PATTERN, key):
57            recursive_methodify(current_function_or_object, gathered_methods)
58
59
60def methodify():
61    """Collect all Methods from the secret object with such names that i gather my Faculty Number"""
62    gathered_methods = {}
63    recursive_methodify(secret, gathered_methods)
64
65    if len(needed_letters) > 0:
66        raise Exception("Not all methods were found")
67
68    return (
69        gathered_methods['F'],
70        gathered_methods['N'],
71        gathered_methods['0'],
72        gathered_methods['M'],
73        gathered_methods['I'],
74        gathered_methods['0'],
75        gathered_methods['6'],
76        gathered_methods['0'],
77        gathered_methods['0'],
78        gathered_methods['0'],
79        gathered_methods['4'],
80        gathered_methods['1'],
81    )

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 69, in methodify
gathered_methods['F'],
KeyError: 'F'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

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

f1import ref1import re
22
3import secret3import secret
44
5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."
66
7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'
8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'
99
10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}
n11gathered_methods = {}n
1211
1312
14def validate_interesting_method(current_function):13def validate_interesting_method(current_function):
15    """Validate if the current function is the one needed"""14    """Validate if the current function is the one needed"""
16    try:15    try:
17        method_check = current_function.__self__16        method_check = current_function.__self__
18        if current_function.__code__.co_argcount == 2:17        if current_function.__code__.co_argcount == 2:
19            if current_function(2) == 4 and current_function(3) == 0:18            if current_function(2) == 4 and current_function(3) == 0:
20                return True19                return True
21        if current_function.__code__.co_argcount == 3:20        if current_function.__code__.co_argcount == 3:
22            if current_function('left ', 'right') == 'left right':21            if current_function('left ', 'right') == 'left right':
23                return True22                return True
24    except AttributeError as e:23    except AttributeError as e:
25        if str(e) == "'function' object has no attribute '__self__'":24        if str(e) == "'function' object has no attribute '__self__'":
26            return True25            return True
27    except TypeError as e:26    except TypeError as e:
28        if str(e) == TYPE_ERROR_TEXT_MESSAGE:27        if str(e) == TYPE_ERROR_TEXT_MESSAGE:
29            return True28            return True
30    except BaseException as e:29    except BaseException as e:
31        if str(e):30        if str(e):
32            return True31            return True
3332
34    return False33    return False
3534
3635
n37def recursive_methodify(curr_object):n36def recursive_methodify(curr_object, gathered_methods):
38    """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""37    """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""
39    if needed_letters == {}:38    if needed_letters == {}:
40        return39        return
4140
42    curr_dict_keys = dir(curr_object)41    curr_dict_keys = dir(curr_object)
4342
44    for key in curr_dict_keys:43    for key in curr_dict_keys:
45        if needed_letters == {}:44        if needed_letters == {}:
46            break45            break
4746
48        current_function_or_object = getattr(curr_object, key)47        current_function_or_object = getattr(curr_object, key)
49        if (48        if (
50                callable(current_function_or_object) and49                callable(current_function_or_object) and
51                re.match(METHOD_NAME_PATTERN, key) and50                re.match(METHOD_NAME_PATTERN, key) and
52                key in needed_letters and51                key in needed_letters and
53                validate_interesting_method(current_function_or_object)52                validate_interesting_method(current_function_or_object)
54        ):53        ):
55            needed_letters.remove(key)54            needed_letters.remove(key)
56            gathered_methods[key] = gathered_methods.get(key, current_function_or_object)55            gathered_methods[key] = gathered_methods.get(key, current_function_or_object)
57        elif re.match(OBJECT_NAME_PATTERN, key):56        elif re.match(OBJECT_NAME_PATTERN, key):
n58            recursive_methodify(current_function_or_object)n57            recursive_methodify(current_function_or_object, gathered_methods)
5958
6059
61def methodify():60def methodify():
62    """Collect all Methods from the secret object with such names that i gather my Faculty Number"""61    """Collect all Methods from the secret object with such names that i gather my Faculty Number"""
tt62    gathered_methods = {}
63    recursive_methodify(secret)63    recursive_methodify(secret, gathered_methods)
6464
65    if len(needed_letters) > 0:65    if len(needed_letters) > 0:
66        raise Exception("Not all methods were found")66        raise Exception("Not all methods were found")
6767
68    return (68    return (
69        gathered_methods['F'],69        gathered_methods['F'],
70        gathered_methods['N'],70        gathered_methods['N'],
71        gathered_methods['0'],71        gathered_methods['0'],
72        gathered_methods['M'],72        gathered_methods['M'],
73        gathered_methods['I'],73        gathered_methods['I'],
74        gathered_methods['0'],74        gathered_methods['0'],
75        gathered_methods['6'],75        gathered_methods['6'],
76        gathered_methods['0'],76        gathered_methods['0'],
77        gathered_methods['0'],77        gathered_methods['0'],
78        gathered_methods['0'],78        gathered_methods['0'],
79        gathered_methods['4'],79        gathered_methods['4'],
80        gathered_methods['1'],80        gathered_methods['1'],
81    )81    )
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import ref1import re
22
3import secret3import secret
44
5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."
66
7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'
8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'
99
10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}
11gathered_methods = {}11gathered_methods = {}
1212
1313
14def validate_interesting_method(current_function):14def validate_interesting_method(current_function):
15    """Validate if the current function is the one needed"""15    """Validate if the current function is the one needed"""
16    try:16    try:
17        method_check = current_function.__self__17        method_check = current_function.__self__
18        if current_function.__code__.co_argcount == 2:18        if current_function.__code__.co_argcount == 2:
19            if current_function(2) == 4 and current_function(3) == 0:19            if current_function(2) == 4 and current_function(3) == 0:
20                return True20                return True
21        if current_function.__code__.co_argcount == 3:21        if current_function.__code__.co_argcount == 3:
22            if current_function('left ', 'right') == 'left right':22            if current_function('left ', 'right') == 'left right':
23                return True23                return True
24    except AttributeError as e:24    except AttributeError as e:
25        if str(e) == "'function' object has no attribute '__self__'":25        if str(e) == "'function' object has no attribute '__self__'":
26            return True26            return True
27    except TypeError as e:27    except TypeError as e:
28        if str(e) == TYPE_ERROR_TEXT_MESSAGE:28        if str(e) == TYPE_ERROR_TEXT_MESSAGE:
29            return True29            return True
30    except BaseException as e:30    except BaseException as e:
31        if str(e):31        if str(e):
32            return True32            return True
3333
34    return False34    return False
3535
3636
37def recursive_methodify(curr_object):37def recursive_methodify(curr_object):
38    """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""38    """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""
39    if needed_letters == {}:39    if needed_letters == {}:
40        return40        return
4141
42    curr_dict_keys = dir(curr_object)42    curr_dict_keys = dir(curr_object)
4343
44    for key in curr_dict_keys:44    for key in curr_dict_keys:
45        if needed_letters == {}:45        if needed_letters == {}:
46            break46            break
4747
48        current_function_or_object = getattr(curr_object, key)48        current_function_or_object = getattr(curr_object, key)
49        if (49        if (
50                callable(current_function_or_object) and50                callable(current_function_or_object) and
51                re.match(METHOD_NAME_PATTERN, key) and51                re.match(METHOD_NAME_PATTERN, key) and
52                key in needed_letters and52                key in needed_letters and
53                validate_interesting_method(current_function_or_object)53                validate_interesting_method(current_function_or_object)
54        ):54        ):
55            needed_letters.remove(key)55            needed_letters.remove(key)
56            gathered_methods[key] = gathered_methods.get(key, current_function_or_object)56            gathered_methods[key] = gathered_methods.get(key, current_function_or_object)
57        elif re.match(OBJECT_NAME_PATTERN, key):57        elif re.match(OBJECT_NAME_PATTERN, key):
58            recursive_methodify(current_function_or_object)58            recursive_methodify(current_function_or_object)
5959
6060
61def methodify():61def methodify():
62    """Collect all Methods from the secret object with such names that i gather my Faculty Number"""62    """Collect all Methods from the secret object with such names that i gather my Faculty Number"""
63    recursive_methodify(secret)63    recursive_methodify(secret)
6464
65    if len(needed_letters) > 0:65    if len(needed_letters) > 0:
66        raise Exception("Not all methods were found")66        raise Exception("Not all methods were found")
6767
68    return (68    return (
t69        gathered_methods['F'].__name__,t69        gathered_methods['F'],
70        gathered_methods['N'].__name__,70        gathered_methods['N'],
71        gathered_methods['0'].__name__,71        gathered_methods['0'],
72        gathered_methods['M'].__name__,72        gathered_methods['M'],
73        gathered_methods['I'].__name__,73        gathered_methods['I'],
74        gathered_methods['0'].__name__,74        gathered_methods['0'],
75        gathered_methods['6'].__name__,75        gathered_methods['6'],
76        gathered_methods['0'].__name__,76        gathered_methods['0'],
77        gathered_methods['0'].__name__,77        gathered_methods['0'],
78        gathered_methods['0'].__name__,78        gathered_methods['0'],
79        gathered_methods['4'].__name__,79        gathered_methods['4'],
80        gathered_methods['1'].__name__,80        gathered_methods['1'],
81    )81    )
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op