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

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

0 точки общо

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

 1class LockPicker_2MI0600361:
 2    """Unlock a lock."""
 3    def __init__(self, lock):
 4        self.lock = lock
 5
 6    def unlock(self):
 7        args = []
 8
 9        while True:
10            try:
11                if self.lock.pick(*args):
12                    return True
13            except Exception as ex:
14                position = ex.position
15                expected = ex.expected
16
17                if position is None:
18                    args = [None] * expected
19                elif isinstance(expected, type):
20                    idx = position - 1
21                    args[idx] = expected
22                else:
23                    idx = position - 1
24                    args[idx] = expected()

Грешка при опит за отключване:
Traceback (most recent call last):
File "/tmp/solution.py", line 11, in unlock
if self.lock.pick(*args):
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 52, in pick
for position, (attempt, slot) in enumerate(zip(attempts, self._slots),
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/timeout_decorator/timeout_decorator.py", line 69, in handler
_raise_exception(timeout_exception, exception_message)
File "/usr/local/lib/python3.12/dist-packages/timeout_decorator/timeout_decorator.py", line 45, in _raise_exception
raise exception()
timeout_decorator.timeout_decorator.TimeoutError: 'Timed Out'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/tmp/test.py", line 98, in <module>
test_unlocker(LockPicker(lock))
File "/usr/local/lib/python3.12/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 77, in test_unlocker
unlocker.unlock()
File "/tmp/solution.py", line 14, in unlock
position = ex.position
^^^^^^^^^^^
AttributeError: 'TimeoutError' object has no attribute 'position'

F
======================================================================
FAIL: test_picking (test.TestLockPicker.test_picking)
Ensure the lock is picked based on already stored boolean var.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 72, in test_picking
self.assertTrue(correct, "Не успя да отключиш.")
AssertionError: False is not true : Не успя да отключиш.

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

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

f1class LockPicker_2MI0600361:f1class LockPicker_2MI0600361:
2    """Unlock a lock."""2    """Unlock a lock."""
3    def __init__(self, lock):3    def __init__(self, lock):
4        self.lock = lock4        self.lock = lock
55
6    def unlock(self):6    def unlock(self):
7        args = []7        args = []
88
9        while True:9        while True:
10            try:10            try:
11                if self.lock.pick(*args):11                if self.lock.pick(*args):
12                    return True12                    return True
13            except Exception as ex:13            except Exception as ex:
14                position = ex.position14                position = ex.position
15                expected = ex.expected15                expected = ex.expected
1616
17                if position is None:17                if position is None:
18                    args = [None] * expected18                    args = [None] * expected
19                elif isinstance(expected, type):19                elif isinstance(expected, type):
20                    idx = position - 120                    idx = position - 1
n21                    if idx < len(args):n
22                        args[idx] = expected21                    args[idx] = expected
23                    else:
24                        args.extend([None] * (idx - len(args)))
25                        args[idx] = expected()
26                else:22                else:
27                    idx = position - 123                    idx = position - 1
t28                    if idx < len(args):t
29                        args[idx] = expected()24                    args[idx] = expected()
30                    else:
31                        args.extend([None] * (idx - len(args)))
32                        args[idx] = expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op