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

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

0 точки общо

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

 1class LockPicker_1MI0600337:
 2    """Unlock my lock."""
 3
 4    def __init__(self, lock):
 5        self._lock = lock
 6
 7    def unlock(self):
 8        DEFAULT_VALUE = 1
 9        password = []
10        while True:
11            try:
12                if self._lock.pick(*password):
13                    return True
14            except TypeError as exc:
15                if exc.position is None:
16                    password = [DEFAULT_VALUE] * exc.expected
17                else:
18                    of_unexpected_type = password[exc.position - 1]
19                    password[exc.position - 1] = exc.expected(of_unexpected_type)
20            except ValueError as exc:
21                    password[exc.position - 1] = exc.expected

Грешка при опит за отключване:
Traceback (most recent call last):
File "/tmp/solution.py", line 12, in unlock
if self._lock.pick(*password):
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 55, in pick
raise LockTypeError("You shall not pass!", position, type(slot))
test.LockTypeError: You shall not pass!

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 19, in unlock
password[exc.position - 1] = exc.expected(of_unexpected_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'int' object is not iterable

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_1MI0600337:f1class LockPicker_1MI0600337:
2    """Unlock my lock."""2    """Unlock my lock."""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        self._lock = lock5        self._lock = lock
66
7    def unlock(self):7    def unlock(self):
8        DEFAULT_VALUE = 18        DEFAULT_VALUE = 1
9        password = []9        password = []
10        while True:10        while True:
11            try:11            try:
12                if self._lock.pick(*password):12                if self._lock.pick(*password):
13                    return True13                    return True
t14            except Exception as exc:t14            except TypeError as exc:
15                if isinstance(exc, TypeError):
16                    if exc.position is None:15                if exc.position is None:
17                        password = [DEFAULT_VALUE] * exc.expected16                    password = [DEFAULT_VALUE] * exc.expected
18                    else:17                else:
19                        of_unexpected_type = password[exc.position - 1]18                    of_unexpected_type = password[exc.position - 1]
20                        password[exc.position - 1] = exc.expected(of_unexpected_type)19                    password[exc.position - 1] = exc.expected(of_unexpected_type)
21                elif isinstance(exc, ValueError):20            except ValueError as exc:
22                    password[exc.position - 1] = exc.expected21                    password[exc.position - 1] = exc.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op