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:
|
| f | 1 | class LockPicker_6MI0600326: | f | 1 | class LockPicker_6MI0600326: |
| 2 | def __init__(self, locker): | 2 | def __init__(self, locker): | ||
| 3 | self.locker = locker | 3 | self.locker = locker | ||
| 4 | 4 | ||||
| 5 | def unlock(self): | 5 | def unlock(self): | ||
| 6 | args = [] | 6 | args = [] | ||
| 7 | 7 | ||||
| 8 | while True: | 8 | while True: | ||
| 9 | try: | 9 | try: | ||
| 10 | self.locker.pick(*args) | 10 | self.locker.pick(*args) | ||
| 11 | return | 11 | 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: | ||
| n | 18 | pos = exc.position - 1 | n | ||
| 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): | ||
| t | 22 | pos = exc.position - 1 | t | ||
| 23 | exp_type = exc.expected | 20 | args[exc.position - 1] = exc.expected | ||
| 24 | args[pos] = exp_type |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | 1 | class ProtectedSection: | n | 1 | class LockPicker_6MI0600326: |
| 2 | def __init__(self, log=(), suppress=()): | 2 | def __init__(self, locker): | ||
| 3 | self.log = log | 3 | self.locker = locker | ||
| 4 | self.suppress = suppress | 4 | |||
| 5 | self.exception = None | 5 | def unlock(self): | ||
| 6 | args = [] | ||||
| 6 | 7 | ||||
| t | 7 | def __enter__(self): | t | 8 | while True: |
| 8 | return self | 9 | 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 True | 11 | return | ||
| 15 | elif exc_type in self.suppress: | 12 | except Exception as exc: | ||
| 16 | return True | 13 | 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||