Предизвикателства > Разбий ни ключалката > Решения > Решението на Енисел Кунч

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

1 точки общо

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

 1class LockPicker_9MI0600401:
 2    def __init__(self, lock):
 3        self.lock = lock
 4
 5    def unlock(self):
 6        args = []
 7        while True:
 8            try:
 9                self.lock.pick(*args)
10                return  #Successfully unlocked
11            except TypeError as ex:
12                if ex.position is None:
13                    args = [None for _ in range(ex.expected)]
14                else:
15                    args[ex.position - 1] = ex.expected()
16
17            except ValueError as ex:
18                args[ex.position - 1] = ex.expected

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

OK

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

f1class LockPicker_9MI0600401:f1class LockPicker_9MI0600401:
2    def __init__(self, lock):2    def __init__(self, lock):
3        self.lock = lock3        self.lock = lock
44
5    def unlock(self):5    def unlock(self):
6        args = []6        args = []
7        while True:7        while True:
8            try:8            try:
9                self.lock.pick(*args)9                self.lock.pick(*args)
10                return  #Successfully unlocked10                return  #Successfully unlocked
11            except TypeError as ex:11            except TypeError as ex:
12                if ex.position is None:12                if ex.position is None:
13                    args = [None for _ in range(ex.expected)]13                    args = [None for _ in range(ex.expected)]
14                else:14                else:
n15                    pos = ex.position - 1 n
16                    expected_type = ex.expected
17                    args[pos] = expected_type()15                    args[ex.position - 1] = ex.expected()
1816
19            except ValueError as ex:17            except ValueError as ex:
t20                pos = ex.position - 1t
21                expected_argument = ex.expected
22                args[pos] = expected_argument18                args[ex.position - 1] = ex.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class LockPicker_9MI0600401:f1class LockPicker_9MI0600401:
2    def __init__(self, lock):2    def __init__(self, lock):
3        self.lock = lock3        self.lock = lock
44
5    def unlock(self):5    def unlock(self):
6        args = []6        args = []
7        while True:7        while True:
8            try:8            try:
t9                if self.lock.pick(*args):t9                self.lock.pick(*args)
10                    return True #Successfully unlocked10                return  #Successfully unlocked
11            except TypeError as ex:11            except TypeError as ex:
12                if ex.position is None:12                if ex.position is None:
13                    args = [None for _ in range(ex.expected)]13                    args = [None for _ in range(ex.expected)]
14                else:14                else:
15                    pos = ex.position - 1 15                    pos = ex.position - 1 
16                    expected_type = ex.expected16                    expected_type = ex.expected
17                    args[pos] = expected_type()17                    args[pos] = expected_type()
1818
19            except ValueError as ex:19            except ValueError as ex:
20                pos = ex.position - 120                pos = ex.position - 1
21                expected_argument = ex.expected21                expected_argument = ex.expected
22                args[pos] = expected_argument22                args[pos] = expected_argument
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op