Предизвикателства > Разбий ни ключалката > Решения > Решението на Тихомир Галов

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

1 точки общо

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

 1class LockPicker_1MI0600118:
 2    def __init__(self, lock):
 3        self.lock = lock
 4
 5    def unlock(self):
 6        args = []
 7        while True:
 8            try:
 9                if self.lock.pick(*args):
10                    print("GG WP")
11                    break  # Unlocked
12            except Exception as ex:
13                if isinstance(ex, TypeError):
14                    if ex.position is None:
15                        expected_arg_count = ex.expected
16                        args = [None] * expected_arg_count
17                    else:
18                        position = ex.position - 1
19                        expected_type = ex.expected
20                        args[position] = self.get_default_value(expected_type)
21                elif isinstance(ex, ValueError):
22                    position = ex.position - 1
23                    expected_value = ex.expected
24                    args[position] = expected_value
25                else:
26                    raise ex # Unknown error
27
28    def get_default_value(self, expected_type):
29        default_values = {
30            int: 0,
31            float: 0.0,
32            str: '',
33            list: [],
34            tuple: (),
35            dict: {},
36            set: set(),
37            bool: False,
38            type(None): None,
39        }
40        if expected_type in default_values:
41            return default_values[expected_type]
42        else:
43            try:
44                return expected_type()
45            except Exception:
46                return None
47
48# Fixed testing data
49# class MyTypeError(TypeError):
50
51#     def __init__(self, position, expected):
52#         self.position = position
53#         self.expected = expected
54
55
56# class MyValueError(ValueError):
57
58#     def __init__(self, position, expected):
59#         self.position = position
60#         self.expected = expected
61
62# class Lock:
63
64#     def __init__(self, *args):
65#         self.ideal = list(*args)
66
67#     def pick(self, *args):
68#         if len(self.ideal) != len(args):
69#             raise MyTypeError(None, len(self.ideal))
70#         for i, el in enumerate(args):
71#             if type(el) is not type(self.ideal[i]):
72#                 raise MyTypeError(i + 1, type(self.ideal[i]))
73#             if el != self.ideal[i]:
74#                 raise MyValueError(i + 1, self.ideal[i])
75#         return True
76
77# if __name__ == '__main__':
78#     lock = Lock(["123", 65])
79#     lockpicker = LockPicker_1MI0600118(lock)
80#     print(lockpicker.unlock())

GG WP
Резултат от контролното:
14/25 верни отговора.
12 точки.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

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