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

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

1 точки общо

0 успешни теста
1 неуспешни теста
Код (fix static method catch all alones in module)

  1import string
  2from inspect import getmembers, isfunction, ismethod
  3
  4import secret
  5
  6
  7class FunctionTester:
  8    def _is_specific_exception_function(
  9        self, function, exception_type, exception_message=None
 10    ):
 11        try:
 12            function()
 13        except exception_type as exception:
 14            is_correct_type = type(exception) is exception_type
 15            if exception_message:
 16                return is_correct_type and str(exception) == exception_message
 17
 18            return is_correct_type
 19        except BaseException:
 20            return False
 21
 22        return False
 23
 24    @staticmethod
 25    def _exception_guard(function):
 26        def wrapper(*args, **kwargs):
 27            try:
 28                return function(*args, **kwargs)
 29            except BaseException:
 30                return False
 31
 32        return wrapper
 33
 34    @_exception_guard
 35    def _is_same_as_function(self, function, stub_function, test_cases):
 36        for args in test_cases:
 37            if function(*args) != stub_function(*args):
 38                return False
 39        return True
 40
 41
 42class ChallengeFunctionTester(FunctionTester):
 43    def is_type_error_function(self, function):
 44        type_error_message = "Опаааааа, тука има нещо нередно."
 45        return self._is_specific_exception_function(
 46            function, TypeError, type_error_message
 47        )
 48
 49    def is_base_error_function(self, function):
 50        return self._is_specific_exception_function(function, BaseException)
 51
 52    def is_int_function(self, function):
 53        def int_function_stub(x):
 54            return x**2 if x % 2 == 0 else 0
 55
 56        int_test_range = 100
 57
 58        test_cases = [
 59            (el,) for el in range(-int_test_range, int_test_range + 1)
 60        ]
 61
 62        return self._is_same_as_function(
 63            function, int_function_stub, test_cases
 64        )
 65
 66    def is_string_function(self, function):
 67        def string_function_stub(left, right):
 68            return left + right
 69
 70        string_test_cases = [
 71            ("a", "b"),
 72            ("abc", "123"),
 73            ("XxX", ""),
 74            ("", "YyY"),
 75            ("", ""),
 76        ]
 77        return self._is_same_as_function(
 78            function, string_function_stub, string_test_cases
 79        )
 80
 81    def is_static_function(self, function):
 82        """Not ideal"""
 83        return not ismethod(function) and function not in [
 84            function for _, function in getmembers(secret)
 85        ]
 86
 87    def is_function_name_ok(self, function):
 88        allowed_characters = list(string.ascii_uppercase + string.digits)
 89        return function.__name__ in allowed_characters
 90
 91    def is_interesting(self, function):
 92        if not self.is_function_name_ok(function):
 93            return False
 94
 95        checkers = [
 96            self.is_type_error_function,
 97            self.is_base_error_function,
 98            self.is_int_function,
 99            self.is_string_function,
100            self.is_static_function,
101        ]
102
103        return any(checker(function) for checker in checkers)
104
105
106def list_all_functions(module_name, member_keyword):
107    stack = [module_name]
108    functions = []
109
110    while stack:
111        current = stack.pop()
112
113        members = getmembers(current)
114        functions.extend([value for _, value in members if isfunction(value)])
115
116        stack.extend(
117            [value for name, value in members if member_keyword in name]
118        )
119
120    return functions
121
122
123def get_interesting_functions():
124    member_keyword = "clue"
125
126    functions = list_all_functions(secret, member_keyword)
127    function_tester = ChallengeFunctionTester()
128
129    return [
130        function
131        for function in functions
132        if function_tester.is_interesting(function)
133    ]
134
135
136def methodify():
137    faculty_number = "FN4MI0600097"
138    interesting_functions = set(get_interesting_functions())
139
140    functions_by_name = {
141        function.__name__: function for function in interesting_functions
142    }
143    fn_functions = [functions_by_name[letter] for letter in faculty_number]
144
145    return tuple(fn_functions)

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 143, in methodify
fn_functions = [functions_by_name[letter] for letter in faculty_number]
File "/tmp/solution.py", line 143, in <listcomp>
fn_functions = [functions_by_name[letter] for letter in faculty_number]
KeyError: 'F'

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

FAILED (errors=1)

Дискусия
Георги Кунчев
19.11.2023 00:04

Няма да сме гадни. Подай число и виж дали вдига на квадрат или е нула. Няма функции, които изглеждат като дефинициите ни, но само понякога.
Теодор Костадинов
18.11.2023 22:24

Как се очаква прецизно да сравняваме функциите за int/ string? Например, функция от сорта на ```return x**2 if x % 2 == 0 and x != 4_000_000 else 0``` би била некоректна, но трудно ще се хване. Или нещо друго бъркам...
История

f1import stringf1import string
2from inspect import getmembers, isfunction, ismethod2from inspect import getmembers, isfunction, ismethod
33
4import secret4import secret
55
66
7class FunctionTester:7class FunctionTester:
8    def _is_specific_exception_function(8    def _is_specific_exception_function(
9        self, function, exception_type, exception_message=None9        self, function, exception_type, exception_message=None
10    ):10    ):
11        try:11        try:
12            function()12            function()
13        except exception_type as exception:13        except exception_type as exception:
14            is_correct_type = type(exception) is exception_type14            is_correct_type = type(exception) is exception_type
15            if exception_message:15            if exception_message:
16                return is_correct_type and str(exception) == exception_message16                return is_correct_type and str(exception) == exception_message
1717
18            return is_correct_type18            return is_correct_type
19        except BaseException:19        except BaseException:
20            return False20            return False
2121
22        return False22        return False
2323
24    @staticmethod24    @staticmethod
25    def _exception_guard(function):25    def _exception_guard(function):
26        def wrapper(*args, **kwargs):26        def wrapper(*args, **kwargs):
27            try:27            try:
28                return function(*args, **kwargs)28                return function(*args, **kwargs)
29            except BaseException:29            except BaseException:
30                return False30                return False
3131
32        return wrapper32        return wrapper
3333
34    @_exception_guard34    @_exception_guard
35    def _is_same_as_function(self, function, stub_function, test_cases):35    def _is_same_as_function(self, function, stub_function, test_cases):
36        for args in test_cases:36        for args in test_cases:
37            if function(*args) != stub_function(*args):37            if function(*args) != stub_function(*args):
38                return False38                return False
39        return True39        return True
4040
4141
42class ChallengeFunctionTester(FunctionTester):42class ChallengeFunctionTester(FunctionTester):
43    def is_type_error_function(self, function):43    def is_type_error_function(self, function):
44        type_error_message = "Опаааааа, тука има нещо нередно."44        type_error_message = "Опаааааа, тука има нещо нередно."
45        return self._is_specific_exception_function(45        return self._is_specific_exception_function(
46            function, TypeError, type_error_message46            function, TypeError, type_error_message
47        )47        )
4848
49    def is_base_error_function(self, function):49    def is_base_error_function(self, function):
50        return self._is_specific_exception_function(function, BaseException)50        return self._is_specific_exception_function(function, BaseException)
5151
52    def is_int_function(self, function):52    def is_int_function(self, function):
53        def int_function_stub(x):53        def int_function_stub(x):
54            return x**2 if x % 2 == 0 else 054            return x**2 if x % 2 == 0 else 0
5555
56        int_test_range = 10056        int_test_range = 100
5757
58        test_cases = [58        test_cases = [
59            (el,) for el in range(-int_test_range, int_test_range + 1)59            (el,) for el in range(-int_test_range, int_test_range + 1)
60        ]60        ]
6161
62        return self._is_same_as_function(62        return self._is_same_as_function(
63            function, int_function_stub, test_cases63            function, int_function_stub, test_cases
64        )64        )
6565
66    def is_string_function(self, function):66    def is_string_function(self, function):
67        def string_function_stub(left, right):67        def string_function_stub(left, right):
68            return left + right68            return left + right
6969
70        string_test_cases = [70        string_test_cases = [
71            ("a", "b"),71            ("a", "b"),
72            ("abc", "123"),72            ("abc", "123"),
73            ("XxX", ""),73            ("XxX", ""),
74            ("", "YyY"),74            ("", "YyY"),
75            ("", ""),75            ("", ""),
76        ]76        ]
77        return self._is_same_as_function(77        return self._is_same_as_function(
78            function, string_function_stub, string_test_cases78            function, string_function_stub, string_test_cases
79        )79        )
8080
81    def is_static_function(self, function):81    def is_static_function(self, function):
t82        return isfunction(function) and not ismethod(function)t82        """Not ideal"""
83        return not ismethod(function) and function not in [
84            function for _, function in getmembers(secret)
85        ]
8386
84    def is_function_name_ok(self, function):87    def is_function_name_ok(self, function):
85        allowed_characters = list(string.ascii_uppercase + string.digits)88        allowed_characters = list(string.ascii_uppercase + string.digits)
86        return function.__name__ in allowed_characters89        return function.__name__ in allowed_characters
8790
88    def is_interesting(self, function):91    def is_interesting(self, function):
89        if not self.is_function_name_ok(function):92        if not self.is_function_name_ok(function):
90            return False93            return False
9194
92        checkers = [95        checkers = [
93            self.is_type_error_function,96            self.is_type_error_function,
94            self.is_base_error_function,97            self.is_base_error_function,
95            self.is_int_function,98            self.is_int_function,
96            self.is_string_function,99            self.is_string_function,
97            self.is_static_function,100            self.is_static_function,
98        ]101        ]
99102
100        return any(checker(function) for checker in checkers)103        return any(checker(function) for checker in checkers)
101104
102105
103def list_all_functions(module_name, member_keyword):106def list_all_functions(module_name, member_keyword):
104    stack = [module_name]107    stack = [module_name]
105    functions = []108    functions = []
106109
107    while stack:110    while stack:
108        current = stack.pop()111        current = stack.pop()
109112
110        members = getmembers(current)113        members = getmembers(current)
111        functions.extend([value for _, value in members if isfunction(value)])114        functions.extend([value for _, value in members if isfunction(value)])
112115
113        stack.extend(116        stack.extend(
114            [value for name, value in members if member_keyword in name]117            [value for name, value in members if member_keyword in name]
115        )118        )
116119
117    return functions120    return functions
118121
119122
120def get_interesting_functions():123def get_interesting_functions():
121    member_keyword = "clue"124    member_keyword = "clue"
122125
123    functions = list_all_functions(secret, member_keyword)126    functions = list_all_functions(secret, member_keyword)
124    function_tester = ChallengeFunctionTester()127    function_tester = ChallengeFunctionTester()
125128
126    return [129    return [
127        function130        function
128        for function in functions131        for function in functions
129        if function_tester.is_interesting(function)132        if function_tester.is_interesting(function)
130    ]133    ]
131134
132135
133def methodify():136def methodify():
134    faculty_number = "FN4MI0600097"137    faculty_number = "FN4MI0600097"
135    interesting_functions = set(get_interesting_functions())138    interesting_functions = set(get_interesting_functions())
136139
137    functions_by_name = {140    functions_by_name = {
138        function.__name__: function for function in interesting_functions141        function.__name__: function for function in interesting_functions
139    }142    }
140    fn_functions = [functions_by_name[letter] for letter in faculty_number]143    fn_functions = [functions_by_name[letter] for letter in faculty_number]
141144
142    return tuple(fn_functions)145    return tuple(fn_functions)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import stringf1import string
n2from inspect import getmembers, isfunctionn2from inspect import getmembers, isfunction, ismethod
33
4import secret4import secret
55
66
7class FunctionTester:7class FunctionTester:
8    def _is_specific_exception_function(8    def _is_specific_exception_function(
9        self, function, exception_type, exception_message=None9        self, function, exception_type, exception_message=None
10    ):10    ):
11        try:11        try:
12            function()12            function()
13        except exception_type as exception:13        except exception_type as exception:
14            is_correct_type = type(exception) is exception_type14            is_correct_type = type(exception) is exception_type
15            if exception_message:15            if exception_message:
16                return is_correct_type and str(exception) == exception_message16                return is_correct_type and str(exception) == exception_message
1717
18            return is_correct_type18            return is_correct_type
19        except BaseException:19        except BaseException:
20            return False20            return False
2121
22        return False22        return False
2323
24    @staticmethod24    @staticmethod
25    def _exception_guard(function):25    def _exception_guard(function):
26        def wrapper(*args, **kwargs):26        def wrapper(*args, **kwargs):
27            try:27            try:
28                return function(*args, **kwargs)28                return function(*args, **kwargs)
29            except BaseException:29            except BaseException:
30                return False30                return False
3131
32        return wrapper32        return wrapper
3333
34    @_exception_guard34    @_exception_guard
35    def _is_same_as_function(self, function, stub_function, test_cases):35    def _is_same_as_function(self, function, stub_function, test_cases):
36        for args in test_cases:36        for args in test_cases:
37            if function(*args) != stub_function(*args):37            if function(*args) != stub_function(*args):
38                return False38                return False
39        return True39        return True
4040
4141
42class ChallengeFunctionTester(FunctionTester):42class ChallengeFunctionTester(FunctionTester):
43    def is_type_error_function(self, function):43    def is_type_error_function(self, function):
44        type_error_message = "Опаааааа, тука има нещо нередно."44        type_error_message = "Опаааааа, тука има нещо нередно."
45        return self._is_specific_exception_function(45        return self._is_specific_exception_function(
46            function, TypeError, type_error_message46            function, TypeError, type_error_message
47        )47        )
4848
49    def is_base_error_function(self, function):49    def is_base_error_function(self, function):
50        return self._is_specific_exception_function(function, BaseException)50        return self._is_specific_exception_function(function, BaseException)
5151
52    def is_int_function(self, function):52    def is_int_function(self, function):
53        def int_function_stub(x):53        def int_function_stub(x):
54            return x**2 if x % 2 == 0 else 054            return x**2 if x % 2 == 0 else 0
5555
56        int_test_range = 10056        int_test_range = 100
5757
58        test_cases = [58        test_cases = [
59            (el,) for el in range(-int_test_range, int_test_range + 1)59            (el,) for el in range(-int_test_range, int_test_range + 1)
60        ]60        ]
6161
62        return self._is_same_as_function(62        return self._is_same_as_function(
63            function, int_function_stub, test_cases63            function, int_function_stub, test_cases
64        )64        )
6565
66    def is_string_function(self, function):66    def is_string_function(self, function):
67        def string_function_stub(left, right):67        def string_function_stub(left, right):
68            return left + right68            return left + right
6969
70        string_test_cases = [70        string_test_cases = [
71            ("a", "b"),71            ("a", "b"),
72            ("abc", "123"),72            ("abc", "123"),
73            ("XxX", ""),73            ("XxX", ""),
74            ("", "YyY"),74            ("", "YyY"),
75            ("", ""),75            ("", ""),
76        ]76        ]
77        return self._is_same_as_function(77        return self._is_same_as_function(
78            function, string_function_stub, string_test_cases78            function, string_function_stub, string_test_cases
79        )79        )
8080
81    def is_static_function(self, function):81    def is_static_function(self, function):
n82        return Falsen82        return isfunction(function) and not ismethod(function)
8383
84    def is_function_name_ok(self, function):84    def is_function_name_ok(self, function):
85        allowed_characters = list(string.ascii_uppercase + string.digits)85        allowed_characters = list(string.ascii_uppercase + string.digits)
86        return function.__name__ in allowed_characters86        return function.__name__ in allowed_characters
8787
88    def is_interesting(self, function):88    def is_interesting(self, function):
89        if not self.is_function_name_ok(function):89        if not self.is_function_name_ok(function):
90            return False90            return False
9191
92        checkers = [92        checkers = [
93            self.is_type_error_function,93            self.is_type_error_function,
94            self.is_base_error_function,94            self.is_base_error_function,
95            self.is_int_function,95            self.is_int_function,
96            self.is_string_function,96            self.is_string_function,
97            self.is_static_function,97            self.is_static_function,
98        ]98        ]
9999
100        return any(checker(function) for checker in checkers)100        return any(checker(function) for checker in checkers)
101101
102102
103def list_all_functions(module_name, member_keyword):103def list_all_functions(module_name, member_keyword):
104    stack = [module_name]104    stack = [module_name]
105    functions = []105    functions = []
106106
107    while stack:107    while stack:
108        current = stack.pop()108        current = stack.pop()
109109
110        members = getmembers(current)110        members = getmembers(current)
111        functions.extend([value for _, value in members if isfunction(value)])111        functions.extend([value for _, value in members if isfunction(value)])
112112
113        stack.extend(113        stack.extend(
114            [value for name, value in members if member_keyword in name]114            [value for name, value in members if member_keyword in name]
115        )115        )
116116
117    return functions117    return functions
118118
119119
120def get_interesting_functions():120def get_interesting_functions():
121    member_keyword = "clue"121    member_keyword = "clue"
122122
123    functions = list_all_functions(secret, member_keyword)123    functions = list_all_functions(secret, member_keyword)
124    function_tester = ChallengeFunctionTester()124    function_tester = ChallengeFunctionTester()
125125
126    return [126    return [
127        function127        function
128        for function in functions128        for function in functions
129        if function_tester.is_interesting(function)129        if function_tester.is_interesting(function)
130    ]130    ]
131131
132132
133def methodify():133def methodify():
134    faculty_number = "FN4MI0600097"134    faculty_number = "FN4MI0600097"
n135    interesting_functions = get_interesting_functions()n135    interesting_functions = set(get_interesting_functions())
136136
137    functions_by_name = {137    functions_by_name = {
138        function.__name__: function for function in interesting_functions138        function.__name__: function for function in interesting_functions
139    }139    }
140    fn_functions = [functions_by_name[letter] for letter in faculty_number]140    fn_functions = [functions_by_name[letter] for letter in faculty_number]
141141
142    return tuple(fn_functions)142    return tuple(fn_functions)
t143                        t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import stringf1import string
2from inspect import getmembers, isfunction2from inspect import getmembers, isfunction
33
4import secret4import secret
55
66
7class FunctionTester:7class FunctionTester:
8    def _is_specific_exception_function(8    def _is_specific_exception_function(
9        self, function, exception_type, exception_message=None9        self, function, exception_type, exception_message=None
10    ):10    ):
11        try:11        try:
12            function()12            function()
13        except exception_type as exception:13        except exception_type as exception:
14            is_correct_type = type(exception) is exception_type14            is_correct_type = type(exception) is exception_type
15            if exception_message:15            if exception_message:
16                return is_correct_type and str(exception) == exception_message16                return is_correct_type and str(exception) == exception_message
1717
18            return is_correct_type18            return is_correct_type
19        except BaseException:19        except BaseException:
20            return False20            return False
2121
22        return False22        return False
2323
24    @staticmethod24    @staticmethod
25    def _exception_guard(function):25    def _exception_guard(function):
26        def wrapper(*args, **kwargs):26        def wrapper(*args, **kwargs):
27            try:27            try:
28                return function(*args, **kwargs)28                return function(*args, **kwargs)
29            except BaseException:29            except BaseException:
30                return False30                return False
3131
32        return wrapper32        return wrapper
3333
34    @_exception_guard34    @_exception_guard
35    def _is_same_as_function(self, function, stub_function, test_cases):35    def _is_same_as_function(self, function, stub_function, test_cases):
36        for args in test_cases:36        for args in test_cases:
37            if function(*args) != stub_function(*args):37            if function(*args) != stub_function(*args):
38                return False38                return False
39        return True39        return True
4040
4141
42class ChallengeFunctionTester(FunctionTester):42class ChallengeFunctionTester(FunctionTester):
43    def is_type_error_function(self, function):43    def is_type_error_function(self, function):
44        type_error_message = "Опаааааа, тука има нещо нередно."44        type_error_message = "Опаааааа, тука има нещо нередно."
45        return self._is_specific_exception_function(45        return self._is_specific_exception_function(
46            function, TypeError, type_error_message46            function, TypeError, type_error_message
47        )47        )
4848
49    def is_base_error_function(self, function):49    def is_base_error_function(self, function):
50        return self._is_specific_exception_function(function, BaseException)50        return self._is_specific_exception_function(function, BaseException)
5151
52    def is_int_function(self, function):52    def is_int_function(self, function):
53        def int_function_stub(x):53        def int_function_stub(x):
54            return x**2 if x % 2 == 0 else 054            return x**2 if x % 2 == 0 else 0
5555
56        int_test_range = 10056        int_test_range = 100
5757
58        test_cases = [58        test_cases = [
59            (el,) for el in range(-int_test_range, int_test_range + 1)59            (el,) for el in range(-int_test_range, int_test_range + 1)
60        ]60        ]
6161
62        return self._is_same_as_function(62        return self._is_same_as_function(
63            function, int_function_stub, test_cases63            function, int_function_stub, test_cases
64        )64        )
6565
66    def is_string_function(self, function):66    def is_string_function(self, function):
67        def string_function_stub(left, right):67        def string_function_stub(left, right):
68            return left + right68            return left + right
6969
70        string_test_cases = [70        string_test_cases = [
71            ("a", "b"),71            ("a", "b"),
72            ("abc", "123"),72            ("abc", "123"),
73            ("XxX", ""),73            ("XxX", ""),
74            ("", "YyY"),74            ("", "YyY"),
75            ("", ""),75            ("", ""),
76        ]76        ]
77        return self._is_same_as_function(77        return self._is_same_as_function(
78            function, string_function_stub, string_test_cases78            function, string_function_stub, string_test_cases
79        )79        )
8080
81    def is_static_function(self, function):81    def is_static_function(self, function):
82        return False82        return False
8383
84    def is_function_name_ok(self, function):84    def is_function_name_ok(self, function):
85        allowed_characters = list(string.ascii_uppercase + string.digits)85        allowed_characters = list(string.ascii_uppercase + string.digits)
86        return function.__name__ in allowed_characters86        return function.__name__ in allowed_characters
8787
88    def is_interesting(self, function):88    def is_interesting(self, function):
89        if not self.is_function_name_ok(function):89        if not self.is_function_name_ok(function):
90            return False90            return False
9191
92        checkers = [92        checkers = [
93            self.is_type_error_function,93            self.is_type_error_function,
94            self.is_base_error_function,94            self.is_base_error_function,
95            self.is_int_function,95            self.is_int_function,
96            self.is_string_function,96            self.is_string_function,
97            self.is_static_function,97            self.is_static_function,
98        ]98        ]
9999
100        return any(checker(function) for checker in checkers)100        return any(checker(function) for checker in checkers)
101101
102102
103def list_all_functions(module_name, member_keyword):103def list_all_functions(module_name, member_keyword):
104    stack = [module_name]104    stack = [module_name]
105    functions = []105    functions = []
106106
107    while stack:107    while stack:
108        current = stack.pop()108        current = stack.pop()
109109
110        members = getmembers(current)110        members = getmembers(current)
111        functions.extend([value for _, value in members if isfunction(value)])111        functions.extend([value for _, value in members if isfunction(value)])
112112
113        stack.extend(113        stack.extend(
114            [value for name, value in members if member_keyword in name]114            [value for name, value in members if member_keyword in name]
115        )115        )
116116
117    return functions117    return functions
118118
119119
120def get_interesting_functions():120def get_interesting_functions():
121    member_keyword = "clue"121    member_keyword = "clue"
122122
123    functions = list_all_functions(secret, member_keyword)123    functions = list_all_functions(secret, member_keyword)
124    function_tester = ChallengeFunctionTester()124    function_tester = ChallengeFunctionTester()
125125
126    return [126    return [
127        function127        function
128        for function in functions128        for function in functions
129        if function_tester.is_interesting(function)129        if function_tester.is_interesting(function)
130    ]130    ]
131131
132132
133def methodify():133def methodify():
t134    faculty_number = "4MI0600097"t134    faculty_number = "FN4MI0600097"
135    interesting_functions = get_interesting_functions()135    interesting_functions = get_interesting_functions()
136136
137    functions_by_name = {137    functions_by_name = {
138        function.__name__: function for function in interesting_functions138        function.__name__: function for function in interesting_functions
139    }139    }
140    fn_functions = [functions_by_name[letter] for letter in faculty_number]140    fn_functions = [functions_by_name[letter] for letter in faculty_number]
141141
142    return tuple(fn_functions)142    return tuple(fn_functions)
143143
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import stringf1import string
2from inspect import getmembers, isfunction2from inspect import getmembers, isfunction
33
4import secret4import secret
55
66
7class FunctionTester:7class FunctionTester:
8    def _is_specific_exception_function(8    def _is_specific_exception_function(
9        self, function, exception_type, exception_message=None9        self, function, exception_type, exception_message=None
10    ):10    ):
11        try:11        try:
12            function()12            function()
13        except exception_type as exception:13        except exception_type as exception:
14            is_correct_type = type(exception) is exception_type14            is_correct_type = type(exception) is exception_type
15            if exception_message:15            if exception_message:
16                return is_correct_type and str(exception) == exception_message16                return is_correct_type and str(exception) == exception_message
1717
18            return is_correct_type18            return is_correct_type
19        except BaseException:19        except BaseException:
20            return False20            return False
2121
22        return False22        return False
2323
24    @staticmethod24    @staticmethod
25    def _exception_guard(function):25    def _exception_guard(function):
26        def wrapper(*args, **kwargs):26        def wrapper(*args, **kwargs):
27            try:27            try:
28                return function(*args, **kwargs)28                return function(*args, **kwargs)
29            except BaseException:29            except BaseException:
30                return False30                return False
3131
32        return wrapper32        return wrapper
3333
34    @_exception_guard34    @_exception_guard
35    def _is_same_as_function(self, function, stub_function, test_cases):35    def _is_same_as_function(self, function, stub_function, test_cases):
36        for args in test_cases:36        for args in test_cases:
37            if function(*args) != stub_function(*args):37            if function(*args) != stub_function(*args):
38                return False38                return False
39        return True39        return True
4040
4141
42class ChallengeFunctionTester(FunctionTester):42class ChallengeFunctionTester(FunctionTester):
43    def is_type_error_function(self, function):43    def is_type_error_function(self, function):
44        type_error_message = "Опаааааа, тука има нещо нередно."44        type_error_message = "Опаааааа, тука има нещо нередно."
45        return self._is_specific_exception_function(45        return self._is_specific_exception_function(
46            function, TypeError, type_error_message46            function, TypeError, type_error_message
47        )47        )
4848
49    def is_base_error_function(self, function):49    def is_base_error_function(self, function):
50        return self._is_specific_exception_function(function, BaseException)50        return self._is_specific_exception_function(function, BaseException)
5151
52    def is_int_function(self, function):52    def is_int_function(self, function):
53        def int_function_stub(x):53        def int_function_stub(x):
54            return x**2 if x % 2 == 0 else 054            return x**2 if x % 2 == 0 else 0
5555
56        int_test_range = 10056        int_test_range = 100
5757
58        test_cases = [58        test_cases = [
59            (el,) for el in range(-int_test_range, int_test_range + 1)59            (el,) for el in range(-int_test_range, int_test_range + 1)
60        ]60        ]
6161
62        return self._is_same_as_function(62        return self._is_same_as_function(
63            function, int_function_stub, test_cases63            function, int_function_stub, test_cases
64        )64        )
6565
66    def is_string_function(self, function):66    def is_string_function(self, function):
n67        def _string_function_stub(left, right):n67        def string_function_stub(left, right):
68            return left + right68            return left + right
6969
70        string_test_cases = [70        string_test_cases = [
71            ("a", "b"),71            ("a", "b"),
72            ("abc", "123"),72            ("abc", "123"),
73            ("XxX", ""),73            ("XxX", ""),
74            ("", "YyY"),74            ("", "YyY"),
75            ("", ""),75            ("", ""),
76        ]76        ]
77        return self._is_same_as_function(77        return self._is_same_as_function(
n78            function, _string_function_stub, string_test_casesn78            function, string_function_stub, string_test_cases
79        )79        )
8080
81    def is_static_function(self, function):81    def is_static_function(self, function):
82        return False82        return False
8383
84    def is_function_name_ok(self, function):84    def is_function_name_ok(self, function):
85        allowed_characters = list(string.ascii_uppercase + string.digits)85        allowed_characters = list(string.ascii_uppercase + string.digits)
86        return function.__name__ in allowed_characters86        return function.__name__ in allowed_characters
8787
88    def is_interesting(self, function):88    def is_interesting(self, function):
89        if not self.is_function_name_ok(function):89        if not self.is_function_name_ok(function):
90            return False90            return False
9191
92        checkers = [92        checkers = [
93            self.is_type_error_function,93            self.is_type_error_function,
94            self.is_base_error_function,94            self.is_base_error_function,
95            self.is_int_function,95            self.is_int_function,
96            self.is_string_function,96            self.is_string_function,
97            self.is_static_function,97            self.is_static_function,
98        ]98        ]
9999
100        return any(checker(function) for checker in checkers)100        return any(checker(function) for checker in checkers)
101101
102102
103def list_all_functions(module_name, member_keyword):103def list_all_functions(module_name, member_keyword):
104    stack = [module_name]104    stack = [module_name]
105    functions = []105    functions = []
106106
107    while stack:107    while stack:
108        current = stack.pop()108        current = stack.pop()
109109
110        members = getmembers(current)110        members = getmembers(current)
111        functions.extend([value for _, value in members if isfunction(value)])111        functions.extend([value for _, value in members if isfunction(value)])
112112
113        stack.extend(113        stack.extend(
114            [value for name, value in members if member_keyword in name]114            [value for name, value in members if member_keyword in name]
115        )115        )
116116
117    return functions117    return functions
118118
119119
120def get_interesting_functions():120def get_interesting_functions():
121    member_keyword = "clue"121    member_keyword = "clue"
122122
123    functions = list_all_functions(secret, member_keyword)123    functions = list_all_functions(secret, member_keyword)
124    function_tester = ChallengeFunctionTester()124    function_tester = ChallengeFunctionTester()
125125
126    return [126    return [
127        function127        function
128        for function in functions128        for function in functions
129        if function_tester.is_interesting(function)129        if function_tester.is_interesting(function)
130    ]130    ]
131131
132132
133def methodify():133def methodify():
n134    faculty_number = "IMMI"  # 4MI0600097n134    faculty_number = "4MI0600097"
135    interesting_functions = get_interesting_functions()135    interesting_functions = get_interesting_functions()
136136
137    functions_by_name = {137    functions_by_name = {
138        function.__name__: function for function in interesting_functions138        function.__name__: function for function in interesting_functions
139    }139    }
140    fn_functions = [functions_by_name[letter] for letter in faculty_number]140    fn_functions = [functions_by_name[letter] for letter in faculty_number]
141141
142    return tuple(fn_functions)142    return tuple(fn_functions)
tt143                        
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import stringf1import string
2from inspect import getmembers, isfunction2from inspect import getmembers, isfunction
33
4import secret4import secret
55
66
7class FunctionTester:7class FunctionTester:
8    def _is_specific_exception_function(8    def _is_specific_exception_function(
9        self, function, exception_type, exception_message=None9        self, function, exception_type, exception_message=None
10    ):10    ):
11        try:11        try:
12            function()12            function()
13        except exception_type as exception:13        except exception_type as exception:
14            is_correct_type = type(exception) is exception_type14            is_correct_type = type(exception) is exception_type
15            if exception_message:15            if exception_message:
16                return is_correct_type and str(exception) == exception_message16                return is_correct_type and str(exception) == exception_message
1717
18            return is_correct_type18            return is_correct_type
19        except BaseException:19        except BaseException:
20            return False20            return False
2121
22        return False22        return False
2323
24    @staticmethod24    @staticmethod
25    def _exception_guard(function):25    def _exception_guard(function):
26        def wrapper(*args, **kwargs):26        def wrapper(*args, **kwargs):
27            try:27            try:
28                return function(*args, **kwargs)28                return function(*args, **kwargs)
29            except BaseException:29            except BaseException:
30                return False30                return False
3131
32        return wrapper32        return wrapper
3333
34    @_exception_guard34    @_exception_guard
35    def _is_same_as_function(self, function, stub_function, test_cases):35    def _is_same_as_function(self, function, stub_function, test_cases):
36        for args in test_cases:36        for args in test_cases:
37            if function(*args) != stub_function(*args):37            if function(*args) != stub_function(*args):
38                return False38                return False
39        return True39        return True
4040
4141
42class ChallengeFunctionTester(FunctionTester):42class ChallengeFunctionTester(FunctionTester):
43    def is_type_error_function(self, function):43    def is_type_error_function(self, function):
44        type_error_message = "Опаааааа, тука има нещо нередно."44        type_error_message = "Опаааааа, тука има нещо нередно."
45        return self._is_specific_exception_function(45        return self._is_specific_exception_function(
46            function, TypeError, type_error_message46            function, TypeError, type_error_message
47        )47        )
4848
49    def is_base_error_function(self, function):49    def is_base_error_function(self, function):
50        return self._is_specific_exception_function(function, BaseException)50        return self._is_specific_exception_function(function, BaseException)
5151
52    def is_int_function(self, function):52    def is_int_function(self, function):
53        def int_function_stub(x):53        def int_function_stub(x):
54            return x**2 if x % 2 == 0 else 054            return x**2 if x % 2 == 0 else 0
5555
56        int_test_range = 10056        int_test_range = 100
5757
58        test_cases = [58        test_cases = [
59            (el,) for el in range(-int_test_range, int_test_range + 1)59            (el,) for el in range(-int_test_range, int_test_range + 1)
60        ]60        ]
6161
62        return self._is_same_as_function(62        return self._is_same_as_function(
63            function, int_function_stub, test_cases63            function, int_function_stub, test_cases
64        )64        )
6565
66    def is_string_function(self, function):66    def is_string_function(self, function):
67        def _string_function_stub(left, right):67        def _string_function_stub(left, right):
68            return left + right68            return left + right
6969
70        string_test_cases = [70        string_test_cases = [
71            ("a", "b"),71            ("a", "b"),
72            ("abc", "123"),72            ("abc", "123"),
73            ("XxX", ""),73            ("XxX", ""),
74            ("", "YyY"),74            ("", "YyY"),
75            ("", ""),75            ("", ""),
76        ]76        ]
77        return self._is_same_as_function(77        return self._is_same_as_function(
78            function, _string_function_stub, string_test_cases78            function, _string_function_stub, string_test_cases
79        )79        )
8080
81    def is_static_function(self, function):81    def is_static_function(self, function):
82        return False82        return False
8383
84    def is_function_name_ok(self, function):84    def is_function_name_ok(self, function):
85        allowed_characters = list(string.ascii_uppercase + string.digits)85        allowed_characters = list(string.ascii_uppercase + string.digits)
86        return function.__name__ in allowed_characters86        return function.__name__ in allowed_characters
8787
88    def is_interesting(self, function):88    def is_interesting(self, function):
89        if not self.is_function_name_ok(function):89        if not self.is_function_name_ok(function):
90            return False90            return False
9191
92        checkers = [92        checkers = [
93            self.is_type_error_function,93            self.is_type_error_function,
94            self.is_base_error_function,94            self.is_base_error_function,
95            self.is_int_function,95            self.is_int_function,
96            self.is_string_function,96            self.is_string_function,
97            self.is_static_function,97            self.is_static_function,
98        ]98        ]
9999
100        return any(checker(function) for checker in checkers)100        return any(checker(function) for checker in checkers)
101101
102102
103def list_all_functions(module_name, member_keyword):103def list_all_functions(module_name, member_keyword):
104    stack = [module_name]104    stack = [module_name]
105    functions = []105    functions = []
106106
107    while stack:107    while stack:
108        current = stack.pop()108        current = stack.pop()
109109
110        members = getmembers(current)110        members = getmembers(current)
111        functions.extend([value for _, value in members if isfunction(value)])111        functions.extend([value for _, value in members if isfunction(value)])
112112
113        stack.extend(113        stack.extend(
114            [value for name, value in members if member_keyword in name]114            [value for name, value in members if member_keyword in name]
115        )115        )
116116
117    return functions117    return functions
118118
119119
120def get_interesting_functions():120def get_interesting_functions():
121    member_keyword = "clue"121    member_keyword = "clue"
122122
123    functions = list_all_functions(secret, member_keyword)123    functions = list_all_functions(secret, member_keyword)
124    function_tester = ChallengeFunctionTester()124    function_tester = ChallengeFunctionTester()
125125
126    return [126    return [
127        function127        function
128        for function in functions128        for function in functions
129        if function_tester.is_interesting(function)129        if function_tester.is_interesting(function)
130    ]130    ]
131131
132132
133def methodify():133def methodify():
134    faculty_number = "IMMI"  # 4MI0600097134    faculty_number = "IMMI"  # 4MI0600097
135    interesting_functions = get_interesting_functions()135    interesting_functions = get_interesting_functions()
136136
137    functions_by_name = {137    functions_by_name = {
138        function.__name__: function for function in interesting_functions138        function.__name__: function for function in interesting_functions
139    }139    }
140    fn_functions = [functions_by_name[letter] for letter in faculty_number]140    fn_functions = [functions_by_name[letter] for letter in faculty_number]
141141
142    return tuple(fn_functions)142    return tuple(fn_functions)
t143 t
144 
145print(methodify())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op