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

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

1 точки общо

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

 1def generate_type(ex_type):
 2    """Generate a default value of the type, if it is an existing type"""
 3    try:
 4        return ex_type()
 5    except Exception:
 6        return None
 7
 8
 9class LockPicker_1MI0600400:
10    def __init__(self, lock):
11        self.lock = lock
12
13    def unlock(self):
14        args = []
15
16        while True:
17            try:
18                if self.lock.pick(*args):
19                    break
20            except Exception as ex:
21                if isinstance(ex, TypeError):
22                    if ex.position is None:
23                        args = [None] * ex.expected
24                    else:
25                        position = ex.position - 1
26                        args[position] = generate_type(ex.expected)
27                elif isinstance(ex, ValueError):
28                    position = ex.position - 1
29                    args[position] = ex.expected

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

OK

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

f1def generate_type(ex_type):f1def generate_type(ex_type):
t2    """Generate a default of value of the type, if it is existing type"""t2    """Generate a default value of the type, if it is an existing type"""
3    try:3    try:
4        return ex_type()4        return ex_type()
5    except Exception:5    except Exception:
6        return None6        return None
77
88
9class LockPicker_1MI0600400:9class LockPicker_1MI0600400:
10    def __init__(self, lock):10    def __init__(self, lock):
11        self.lock = lock11        self.lock = lock
1212
13    def unlock(self):13    def unlock(self):
14        args = []14        args = []
1515
16        while True:16        while True:
17            try:17            try:
18                if self.lock.pick(*args):18                if self.lock.pick(*args):
19                    break19                    break
20            except Exception as ex:20            except Exception as ex:
21                if isinstance(ex, TypeError):21                if isinstance(ex, TypeError):
22                    if ex.position is None:22                    if ex.position is None:
23                        args = [None] * ex.expected23                        args = [None] * ex.expected
24                    else:24                    else:
25                        position = ex.position - 125                        position = ex.position - 1
26                        args[position] = generate_type(ex.expected)26                        args[position] = generate_type(ex.expected)
27                elif isinstance(ex, ValueError):27                elif isinstance(ex, ValueError):
28                    position = ex.position - 128                    position = ex.position - 1
29                    args[position] = ex.expected29                    args[position] = ex.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def _generate_type(ex_type):n1def generate_type(ex_type):
2    """Generate a default of value of the type, if it is existing type"""2    """Generate a default of value of the type, if it is existing type"""
3    try:3    try:
4        return ex_type()4        return ex_type()
5    except Exception:5    except Exception:
6        return None6        return None
77
88
9class LockPicker_1MI0600400:9class LockPicker_1MI0600400:
10    def __init__(self, lock):10    def __init__(self, lock):
11        self.lock = lock11        self.lock = lock
1212
13    def unlock(self):13    def unlock(self):
14        args = []14        args = []
1515
16        while True:16        while True:
17            try:17            try:
18                if self.lock.pick(*args):18                if self.lock.pick(*args):
19                    break19                    break
20            except Exception as ex:20            except Exception as ex:
21                if isinstance(ex, TypeError):21                if isinstance(ex, TypeError):
22                    if ex.position is None:22                    if ex.position is None:
23                        args = [None] * ex.expected23                        args = [None] * ex.expected
24                    else:24                    else:
25                        position = ex.position - 125                        position = ex.position - 1
t26                        args[position] = _generate_type(ex.expected)t26                        args[position] = generate_type(ex.expected)
27                elif isinstance(ex, ValueError):27                elif isinstance(ex, ValueError):
28                    position = ex.position - 128                    position = ex.position - 1
29                    args[position] = ex.expected29                    args[position] = ex.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op