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

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

0 точки общо

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

 1class LockPicker_0MI0600362:
 2
 3    def __init__(self, lock):
 4        self.lock = lock
 5
 6    def unlock(self):
 7        arg_count = self._get_arg_count()
 8        arg_types = self._get_arg_types(arg_count)
 9        correct_args = self._get_arg_values(arg_types)
10        self.lock.pick(*correct_args)
11
12    def _get_arg_count(self):
13        try:
14            self.lock.pick()
15        except TypeError as e:
16            return e.expected 
17        
18    def _get_arg_types(self, arg_count):
19        types = []
20        args = [None] * arg_count
21
22        for i in range(arg_count):
23            try:
24                self.lock.pick(*args)
25            except TypeError as e:
26                if e.position == i + 1:
27                    types.append(e.expected)
28                    args[i] = self._default_value(e.expected)
29
30        return types
31    
32    def _get_arg_values(self, arg_types):
33        args = [self._default_value(t) for t in arg_types]
34        for i in range(len(arg_types)):
35            try:
36                self.lock.pick(*args)
37                return args
38            except ValueError as e:
39                if e.position == i + 1:
40                    args[i] = e.expected
41                    break
42
43        return args
44
45    def _default_value(self, t):
46        if t == int:
47            return 0
48        elif t == str:
49            return ""
50        elif t == bool:
51            return False
52        elif t == float:
53            return 0.0
54        elif t == list:
55            return []
56        elif t == dict:
57            return {}
58        elif t == tuple:
59            return ()
60        elif t == set:
61            return set()
62        else:
63            raise ValueError(f"Unsupported type: {t}")

Грешка при опит за отключване:
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 10, in unlock
self.lock.pick(*correct_args)
File "/tmp/test.py", line 59, in pick
raise LockValueError("You shall not pass!", position, slot)
test.LockValueError: You shall not pass!

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)

Дискусия
Георги Кунчев
13.11.2024 13:55

Да, вместо `self._default_value(e.expected)` можеш направо `e.expected()`
Камелия Михайлова
13.11.2024 13:34

Тоест и функцията default_values е излишна така ли?
История
Това решение има само една версия.