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

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

1 точки общо

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

 1class LockPicker_4MI0600234:
 2
 3    def __init__(self, lock):
 4        self.lock = lock
 5
 6    def unlock(self):
 7        args = []
 8        args_count = 0
 9        while True:
10            try:
11                self.lock.pick(*args)
12            except TypeError as ex:
13                if ex.position is None:
14                    args_count = ex.expected
15                    args = [None] * args_count
16                else:
17                    args[ex.position - 1] = ex.expected()
18            except ValueError as ex:
19                args[ex.position - 1] = ex.expected
20            else:
21                break

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

OK

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

f1class LockPicker_4MI0600234:f1class LockPicker_4MI0600234:
22
3    def __init__(self, lock):3    def __init__(self, lock):
4        self.lock = lock4        self.lock = lock
55
6    def unlock(self):6    def unlock(self):
7        args = []7        args = []
8        args_count = 08        args_count = 0
9        while True:9        while True:
10            try:10            try:
11                self.lock.pick(*args)11                self.lock.pick(*args)
12            except TypeError as ex:12            except TypeError as ex:
13                if ex.position is None:13                if ex.position is None:
14                    args_count = ex.expected14                    args_count = ex.expected
15                    args = [None] * args_count15                    args = [None] * args_count
16                else:16                else:
n17                    args[ex.position - 1] = self.__generate_value_of_type__(ex.expected)n17                    args[ex.position - 1] = ex.expected()
18            except ValueError as ex:18            except ValueError as ex:
19                args[ex.position - 1] = ex.expected19                args[ex.position - 1] = ex.expected
20            else:20            else:
21                break21                break
t22 t
23    def __generate_value_of_type__(self, expected_type):
24        if expected_type is int:
25            return 1
26        elif expected_type is float:
27            return 0.1
28        elif expected_type is str:
29            return ''
30        elif expected_type is bool:
31            return False
32        elif expected_type is list:
33            return []
34        elif expected_type is tuple:
35            return ()
36        elif expected_type is set:
37            return set()
38        elif expected_type is dict:
39            return dict()
40        elif expected_type is complex:
41            return 1 + 1j
42        elif expected_type is frozenset:
43            return frozenset()
44        else:
45            return None
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op