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

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

0 точки общо

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

 1class LockPicker_3MI0600212:
 2    def __init__(self, lock):
 3        self.lock = lock
 4    
 5    def unlock(self):
 6        pick_arguments = [None]
 7        while True:
 8            try:
 9                if self.lock.pick(*pick_arguments):
10                    return True
11            except TypeError as exception: 
12                if exception.position is None: # the first step is here  to get the right count of args
13                    if len(pick_arguments) < exception.expected:
14                        pick_arguments *= exception.expected
15                if isinstance(exception.position, int):
16                     expected_type = exception.expected #type of current arg
17                     pick_arguments[exception.position - 1] = expected_type(pick_arguments[exception.position - 1]) #cast to expected type
18            except ValueError as exception:
19                if isinstance(exception.position, int): 
20                    pick_arguments[exception.position - 1] = exception.expected #set expected value at curr position
21
22
23                

Грешка при опит за отключване:
Traceback (most recent call last):
File "/tmp/solution.py", line 9, in unlock
if self.lock.pick(*pick_arguments):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 17, in unlock
pick_arguments[exception.position - 1] = expected_type(pick_arguments[exception.position - 1]) #cast to expected type
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

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)

Дискусия
История
Това решение има само една версия.