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

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

1 точки общо

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

 1RESULTS = {
 2    "0MI0600302": {
 3        "correct": "",
 4        "points": ""
 5    }
 6}
 7
 8
 9class LockPicker_0MI0600302:
10    def __init__(self, lock):
11        self.lock = lock
12
13    def unlock(self):
14        args = []
15        while True:
16            try:
17                result = self.lock.pick(*args)
18                if result:
19                    return args
20            except TypeError as error:
21                position = error.position
22                expected = error.expected
23                if position is None:
24                    args = [None] * expected
25                else:
26                    args[position - 1] = expected()
27            except ValueError as error:
28                position = error.position
29                expected = error.expected
30                args[position - 1] = expected

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

OK

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

n1# class NotAccurateArgsLen(TypeError):n1RESULTS = {
2#     def __init__(self, message, position, expected):2    "0MI0600302": {
3#         super().__init__(message)3        "correct": "",
4#         self.position = position4        "points": ""
5#         self.expected = expected5    }
6#6}
7#
8# class NotAccurateTypeOnPosition(TypeError):
9#     def __init__(self, message, position, expected):
10#         super().__init__(message)
11#         self.position = position
12#         self.expected = expected
13#
14#
15# class NotAccurateValue(ValueError):
16#     def __init__(self, message, position, expected):
17#         super().__init__(message)
18#         self.position = position
19#         self.expected = expected
20#
21#
22# class Lock:
23#     def __init__(self, *private_args):
24#         self.__private_args = private_args
25#
26#     def pick(self, *guess_args):
27#         if len(self.__private_args) != len(guess_args):
28#             raise NotAccurateArgsLen("some message", None, len(self.__private_args))
29#
30#         self.check_for_invalid_type(guess_args)
31#         self.check_for_invalid_value(guess_args)
32#
33#         return True
34#
35#     def check_for_invalid_type(self, guess_args):
36#         for i in range(len(self.__private_args)):
37#             if type(self.__private_args[i]) is not type(guess_args[i]):
38#                 raise NotAccurateTypeOnPosition("some message", i + 1, type(self.__private_args[i]))
39#
40#     def check_for_invalid_value(self, guess_args):
41#         for i in range(len(self.__private_args)):
42#             if self.__private_args[i] != guess_args[i]:
43#                 raise NotAccurateValue("some message", i + 1, self.__private_args[i])
447
458
46class LockPicker_0MI0600302:9class LockPicker_0MI0600302:
47    def __init__(self, lock):10    def __init__(self, lock):
48        self.lock = lock11        self.lock = lock
4912
50    def unlock(self):13    def unlock(self):
51        args = []14        args = []
52        while True:15        while True:
53            try:16            try:
54                result = self.lock.pick(*args)17                result = self.lock.pick(*args)
55                if result:18                if result:
56                    return args19                    return args
57            except TypeError as error:20            except TypeError as error:
58                position = error.position21                position = error.position
59                expected = error.expected22                expected = error.expected
60                if position is None:23                if position is None:
61                    args = [None] * expected24                    args = [None] * expected
62                else:25                else:
63                    args[position - 1] = expected()26                    args[position - 1] = expected()
64            except ValueError as error:27            except ValueError as error:
65                position = error.position28                position = error.position
66                expected = error.expected29                expected = error.expected
67                args[position - 1] = expected30                args[position - 1] = expected
t68 t
69 
70# lock = Lock("lelq ginka", 1, [1, 2, 3], ("tupulche",))
71# loader = LockPicker_0MI0600302(lock)
72# print(loader.unlock())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1# class NotAccurateArgsLen(TypeError):f1# class NotAccurateArgsLen(TypeError):
2#     def __init__(self, message, position, expected):2#     def __init__(self, message, position, expected):
3#         super().__init__(message)3#         super().__init__(message)
4#         self.position = position4#         self.position = position
5#         self.expected = expected5#         self.expected = expected
6#6#
7#7#
8# class NotAccurateTypeOnPosition(TypeError):8# class NotAccurateTypeOnPosition(TypeError):
9#     def __init__(self, message, position, expected):9#     def __init__(self, message, position, expected):
10#         super().__init__(message)10#         super().__init__(message)
11#         self.position = position11#         self.position = position
12#         self.expected = expected12#         self.expected = expected
13#13#
14#14#
15# class NotAccurateValue(ValueError):15# class NotAccurateValue(ValueError):
16#     def __init__(self, message, position, expected):16#     def __init__(self, message, position, expected):
17#         super().__init__(message)17#         super().__init__(message)
18#         self.position = position18#         self.position = position
19#         self.expected = expected19#         self.expected = expected
20#20#
21#21#
22# class Lock:22# class Lock:
23#     def __init__(self, *private_args):23#     def __init__(self, *private_args):
24#         self.__private_args = private_args24#         self.__private_args = private_args
25#25#
26#     def pick(self, *guess_args):26#     def pick(self, *guess_args):
27#         if len(self.__private_args) != len(guess_args):27#         if len(self.__private_args) != len(guess_args):
28#             raise NotAccurateArgsLen("some message", None, len(self.__private_args))28#             raise NotAccurateArgsLen("some message", None, len(self.__private_args))
29#29#
30#         self.check_for_invalid_type(guess_args)30#         self.check_for_invalid_type(guess_args)
31#         self.check_for_invalid_value(guess_args)31#         self.check_for_invalid_value(guess_args)
32#32#
nn33#         return True
34#
33#     def check_for_invalid_type(self, guess_args):35#     def check_for_invalid_type(self, guess_args):
34#         for i in range(len(self.__private_args)):36#         for i in range(len(self.__private_args)):
35#             if type(self.__private_args[i]) is not type(guess_args[i]):37#             if type(self.__private_args[i]) is not type(guess_args[i]):
36#                 raise NotAccurateTypeOnPosition("some message", i + 1, type(self.__private_args[i]))38#                 raise NotAccurateTypeOnPosition("some message", i + 1, type(self.__private_args[i]))
37#39#
38#     def check_for_invalid_value(self, guess_args):40#     def check_for_invalid_value(self, guess_args):
39#         for i in range(len(self.__private_args)):41#         for i in range(len(self.__private_args)):
40#             if self.__private_args[i] != guess_args[i]:42#             if self.__private_args[i] != guess_args[i]:
41#                 raise NotAccurateValue("some message", i + 1, self.__private_args[i])43#                 raise NotAccurateValue("some message", i + 1, self.__private_args[i])
4244
4345
44class LockPicker_0MI0600302:46class LockPicker_0MI0600302:
45    def __init__(self, lock):47    def __init__(self, lock):
46        self.lock = lock48        self.lock = lock
4749
48    def unlock(self):50    def unlock(self):
49        args = []51        args = []
50        while True:52        while True:
51            try:53            try:
52                result = self.lock.pick(*args)54                result = self.lock.pick(*args)
nn55                if result:
53                return args56                    return args
54            except TypeError as error:57            except TypeError as error:
55                position = error.position58                position = error.position
56                expected = error.expected59                expected = error.expected
57                if position is None:60                if position is None:
58                    args = [None] * expected61                    args = [None] * expected
59                else:62                else:
60                    args[position - 1] = expected()63                    args[position - 1] = expected()
61            except ValueError as error:64            except ValueError as error:
62                position = error.position65                position = error.position
63                expected = error.expected66                expected = error.expected
64                args[position - 1] = expected67                args[position - 1] = expected
tt68 
69 
70# lock = Lock("lelq ginka", 1, [1, 2, 3], ("tupulche",))
71# loader = LockPicker_0MI0600302(lock)
72# print(loader.unlock())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op