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

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

1 точки общо

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

 1class LockPicker_6MI0600326:
 2    def __init__(self, locker):
 3        self.locker = locker
 4    
 5    def unlock(self):
 6        args = []
 7
 8        while True:
 9            try:
10                self.locker.pick(*args)
11                return
12            except Exception as exc:
13                if isinstance(exc, TypeError):
14                    if exc.position is None:
15                        while len(args) < exc.expected:
16                            args.append(None)
17                    else:
18                        args[exc.position - 1] = exc.expected()
19                elif isinstance(exc, ValueError):
20                    args[exc.position - 1] = exc.expected

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

OK

Дискусия
Виктор Бечев
13.11.2024 15:26

Тъкмо се подготвях да коментирам предното "решение". :grin:
История

f1class LockPicker_6MI0600326:f1class LockPicker_6MI0600326:
2    def __init__(self, locker):2    def __init__(self, locker):
3        self.locker = locker3        self.locker = locker
4    4    
5    def unlock(self):5    def unlock(self):
6        args = []6        args = []
77
8        while True:8        while True:
9            try:9            try:
10                self.locker.pick(*args)10                self.locker.pick(*args)
11                return11                return
12            except Exception as exc:12            except Exception as exc:
13                if isinstance(exc, TypeError):13                if isinstance(exc, TypeError):
14                    if exc.position is None:14                    if exc.position is None:
15                        while len(args) < exc.expected:15                        while len(args) < exc.expected:
16                            args.append(None)16                            args.append(None)
17                    else:17                    else:
n18                        pos = exc.position - 1n
19                        exp_type = exc.expected
20                        args[pos] = exp_type()18                        args[exc.position - 1] = exc.expected()
21                elif isinstance(exc, ValueError):19                elif isinstance(exc, ValueError):
t22                    pos = exc.position - 1t
23                    exp_type = exc.expected20                    args[exc.position - 1] = exc.expected
24                    args[pos] = exp_type
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1class ProtectedSection:n1class LockPicker_6MI0600326:
2    def __init__(self, log=(), suppress=()):2    def __init__(self, locker):
3        self.log = log3        self.locker = locker
4        self.suppress = suppress4    
5        self.exception = None5    def unlock(self):
6        args = []
67
t7    def __enter__(self):t8        while True:
8        return self9            try:
9    10                self.locker.pick(*args)
10    def __exit__(self, exc_type, exc_val, exc_tb):
11        if exc_type:
12            if exc_type in self.log:
13                self.exception = exc_val
14                return True11                return
15            elif exc_type in self.suppress:12            except Exception as exc:
16                return True13                if isinstance(exc, TypeError):
17        return False      14                    if exc.position is None:
15                        while len(args) < exc.expected:
16                            args.append(None)
17                    else:
18                        pos = exc.position - 1
19                        exp_type = exc.expected
20                        args[pos] = exp_type()
21                elif isinstance(exc, ValueError):
22                    pos = exc.position - 1
23                    exp_type = exc.expected
24                    args[pos] = exp_type
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op