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

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

4 точки общо

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

 1import random
 2from inspect import getattr_static
 3
 4import secret
 5
 6
 7fn = 'FN6MI0600150'
 8
 9
10def catch_errors(fn):
11    def wrapper(*args, **kwargs):
12        try:
13            check_result = fn(*args, **kwargs)
14        except TypeError as error:
15            return str(error) == 'Опаааааа, тука има нещо нередно.'
16        except BaseException as error:
17            return type(error) == BaseException
18
19        return check_result
20    return wrapper
21
22
23@catch_errors
24def call_no_args(method):
25    method()
26
27
28@catch_errors
29def call_one_arg(method):
30    value = (int)(random.random()*1000 + 666)
31
32    even_value = value if value % 2 == 0 else value + 1
33    odd_value = value if value % 2 == 1 else value + 1
34
35    if method(even_value) != even_value**2:
36        return False
37
38    if method(odd_value) != 0:
39        return False
40
41    return True
42
43
44@catch_errors
45def call_two_args(method):
46    return method(right='ba', left='ab') == 'abba'
47
48
49def is_method_interesting(method):
50    return True in (call_no_args(method), call_one_arg(method), call_two_args(method))
51
52
53def find_clue(object, interesting_methods):
54
55    dictionary = dir(object)
56    clues = []
57
58    for item in dictionary:
59        attr = getattr(object, item)
60
61        if 'clue' not in item:
62            if len(item) == 1 and callable(attr) and item in fn:
63                if isinstance(getattr_static(object, item), staticmethod):
64                    interesting_methods[item] = attr
65                    continue
66
67                if is_method_interesting(attr):
68                    interesting_methods[item] = attr
69
70            continue
71
72        clues.append(getattr(object, item))
73
74    for clue in clues:
75        find_clue(clue, interesting_methods)
76
77
78def methodify():
79    interesting_methods = {}
80
81    find_clue(secret, interesting_methods)
82
83    return tuple(interesting_methods[key] for key in fn)

Резултат от контролното:
29/30 верни отговора.
30 точки.
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

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