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

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

1 точки общо

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

 1RESULTS = {
 2    "0MI0600128": { 
 3        "correct": "some_random_string",
 4        "points": "another_random_string",
 5    }
 6}
 7
 8class LockPicker_0MI0600128:
 9    def __init__(self, lock):
10        self.lock = lock  
11
12    def unlock(self):
13        attempt = []  
14        num_args = None  
15
16        while True:
17            try:
18                if num_args is not None:
19                    while len(attempt) < num_args:
20                        attempt.append(None)
21                result = self.lock.pick(*attempt)
22                return result  
23
24            except Exception as ex:
25                if isinstance(ex, TypeError):
26                    if ex.position is None:
27                        num_args = ex.expected
28                        attempt = [None] * num_args  
29                    else:
30                        if callable(ex.expected):
31                            attempt[ex.position - 1] = ex.expected()
32                        else:
33                            attempt[ex.position - 1] = ex.expected
34
35                elif isinstance(ex, ValueError):
36                    attempt[ex.position - 1] = ex.expected

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

OK

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

t1RESULTS = {t1RESULTS = {
2    "0MI0600128": { 2    "0MI0600128": { 
3        "correct": "some_random_string",3        "correct": "some_random_string",
4        "points": "another_random_string",4        "points": "another_random_string",
5    }5    }
6}6}
77
8class LockPicker_0MI0600128:8class LockPicker_0MI0600128:
9    def __init__(self, lock):9    def __init__(self, lock):
10        self.lock = lock  10        self.lock = lock  
1111
12    def unlock(self):12    def unlock(self):
13        attempt = []  13        attempt = []  
14        num_args = None  14        num_args = None  
1515
16        while True:16        while True:
17            try:17            try:
18                if num_args is not None:18                if num_args is not None:
19                    while len(attempt) < num_args:19                    while len(attempt) < num_args:
20                        attempt.append(None)20                        attempt.append(None)
21                result = self.lock.pick(*attempt)21                result = self.lock.pick(*attempt)
22                return result  22                return result  
2323
24            except Exception as ex:24            except Exception as ex:
25                if isinstance(ex, TypeError):25                if isinstance(ex, TypeError):
26                    if ex.position is None:26                    if ex.position is None:
27                        num_args = ex.expected27                        num_args = ex.expected
28                        attempt = [None] * num_args  28                        attempt = [None] * num_args  
29                    else:29                    else:
30                        if callable(ex.expected):30                        if callable(ex.expected):
31                            attempt[ex.position - 1] = ex.expected()31                            attempt[ex.position - 1] = ex.expected()
32                        else:32                        else:
33                            attempt[ex.position - 1] = ex.expected33                            attempt[ex.position - 1] = ex.expected
3434
35                elif isinstance(ex, ValueError):35                elif isinstance(ex, ValueError):
36                    attempt[ex.position - 1] = ex.expected36                    attempt[ex.position - 1] = ex.expected
3737
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1RESULTS = {
2    "0MI0600128": { 
3        "correct": "some_random_string",
4        "points": "another_random_string",
5    }
6}
7 
1class LockPicker_0MI0600128:8class LockPicker_0MI0600128:
2    def __init__(self, lock):9    def __init__(self, lock):
3        self.lock = lock  10        self.lock = lock  
411
5    def unlock(self):12    def unlock(self):
n6        attempt = [] n13        attempt = []  
7        num_args = None  14        num_args = None  
nn15 
8        while True:16        while True:
9            try:17            try:
10                if num_args is not None:18                if num_args is not None:
11                    while len(attempt) < num_args:19                    while len(attempt) < num_args:
12                        attempt.append(None)20                        attempt.append(None)
13                result = self.lock.pick(*attempt)21                result = self.lock.pick(*attempt)
14                return result  22                return result  
nn23 
15            except Exception as ex:24            except Exception as ex:
16                if isinstance(ex, TypeError):25                if isinstance(ex, TypeError):
17                    if ex.position is None:26                    if ex.position is None:
18                        num_args = ex.expected27                        num_args = ex.expected
n19                        attempt = [None] * num_argsn28                        attempt = [None] * num_args  
20                    else:29                    else:
nn30                        if callable(ex.expected):
21                        attempt[ex.position - 1] = ex.expected()31                            attempt[ex.position - 1] = ex.expected()
32                        else:
33                            attempt[ex.position - 1] = ex.expected
34 
22                elif isinstance(ex, ValueError):35                elif isinstance(ex, ValueError):
23                    attempt[ex.position - 1] = ex.expected36                    attempt[ex.position - 1] = ex.expected
tt37 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op