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

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

1 точки общо

1 успешни теста
0 неуспешни теста
Код (python няма макс число ffs)

 1class LockPicker_31593:
 2    def __init__(self, lock):
 3        self.lock = lock
 4
 5    def unlock(self):
 6        try:
 7            self.lock.pick()
 8        except Exception as ex:
 9            self.lock_args = [None for _ in range(ex.expected)]
10
11        while True:
12            try:
13                self.lock.pick(*self.lock_args)
14                return
15            except Exception as ex:
16                if isinstance(ex, TypeError):
17                    self.lock_args[ex.position - 1] = ex.expected()
18                elif isinstance(ex, ValueError):
19                    self.lock_args[ex.position - 1] = ex.expected

Резултат от контролното:
18/25 верни отговора.
15 точки.
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

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

По принцип няма да тестваме с такъв случай, затова ще си позволя да ти споделя потенциален проблем. Ако ключалката не очаква никакви аргументи, `self.lock_args` ще ти остане недефинирано и после влизаш в безкраен цикъл. Пак да кажа - не е нужно да го оправяш, защото няма да тестваме за празна ключалка.
История

t1class LockPicker_FN31593:t1class LockPicker_31593:
2    def __init__(self, lock):2    def __init__(self, lock):
3        self.lock = lock3        self.lock = lock
44
5    def unlock(self):5    def unlock(self):
6        try:6        try:
7            self.lock.pick()7            self.lock.pick()
8        except Exception as ex:8        except Exception as ex:
9            self.lock_args = [None for _ in range(ex.expected)]9            self.lock_args = [None for _ in range(ex.expected)]
1010
11        while True:11        while True:
12            try:12            try:
13                self.lock.pick(*self.lock_args)13                self.lock.pick(*self.lock_args)
14                return14                return
15            except Exception as ex:15            except Exception as ex:
16                if isinstance(ex, TypeError):16                if isinstance(ex, TypeError):
17                    self.lock_args[ex.position - 1] = ex.expected()17                    self.lock_args[ex.position - 1] = ex.expected()
18                elif isinstance(ex, ValueError):18                elif isinstance(ex, ValueError):
19                    self.lock_args[ex.position - 1] = ex.expected19                    self.lock_args[ex.position - 1] = ex.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1class LockPicker_FN31593:t1class LockPicker_FN31593:
2    def __init__(self, lock):2    def __init__(self, lock):
3        self.lock = lock3        self.lock = lock
44
5    def unlock(self):5    def unlock(self):
6        try:6        try:
7            self.lock.pick()7            self.lock.pick()
8        except Exception as ex:8        except Exception as ex:
9            self.lock_args = [None for _ in range(ex.expected)]9            self.lock_args = [None for _ in range(ex.expected)]
1010
11        while True:11        while True:
12            try:12            try:
13                self.lock.pick(*self.lock_args)13                self.lock.pick(*self.lock_args)
14                return14                return
15            except Exception as ex:15            except Exception as ex:
16                if isinstance(ex, TypeError):16                if isinstance(ex, TypeError):
17                    self.lock_args[ex.position - 1] = ex.expected()17                    self.lock_args[ex.position - 1] = ex.expected()
18                elif isinstance(ex, ValueError):18                elif isinstance(ex, ValueError):
19                    self.lock_args[ex.position - 1] = ex.expected19                    self.lock_args[ex.position - 1] = ex.expected
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op