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

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

1 точки общо

1 успешни теста
0 неуспешни теста
Код (Real Solution)

 1class LockPicker_gkunchev:
 2    """Pick a lock. It's that simple!"""
 3
 4    def __init__(self, lock):
 5        """Store the lock passed when initializing."""
 6        self._lock = lock
 7
 8    def unlock(self):
 9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list
11        # to determine the expected argument count
12        combination = []
13        while True:
14            try:
15                self._lock.pick(*combination)
16            except TypeError as ex:
17                # This case defines the number of expected arguments
18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance
23                # from the expected type
24                else:
25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found
27            # pass the expected value instead
28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked
31            else:
32                break

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

OK

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

n1class LockPicker_gkunchev1:n1class LockPicker_gkunchev:
2    """Pick a lock. It's that simple!"""2    """Pick a lock. It's that simple!"""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        """Store the lock passed when initializing."""5        """Store the lock passed when initializing."""
6        self._lock = lock6        self._lock = lock
77
8    def unlock(self):8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list10        # Start by passing empty argument list
11        # to determine the expected argument count11        # to determine the expected argument count
12        combination = []12        combination = []
13        while True:13        while True:
14            try:14            try:
15                self._lock.pick(*combination)15                self._lock.pick(*combination)
16            except TypeError as ex:16            except TypeError as ex:
17                # This case defines the number of expected arguments17                # This case defines the number of expected arguments
18                if ex.position is None:18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance22                # An unexpected type was passed so pass empty instance
23                # from the expected type23                # from the expected type
24                else:24                else:
25                    combination[ex.position - 1] = ex.expected()25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found26            # An unexpected value of the correct type was found
27            # pass the expected value instead27            # pass the expected value instead
28            except ValueError as ex:28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked30            # No exception means that the lock is unlocked
31            else:31            else:
32                break32                break
t33 t
34print('This is a test')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1class LockPicker_gkunchev1:t1class LockPicker_gkunchev1:
2    """Pick a lock. It's that simple!"""2    """Pick a lock. It's that simple!"""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        """Store the lock passed when initializing."""5        """Store the lock passed when initializing."""
6        self._lock = lock6        self._lock = lock
77
8    def unlock(self):8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list10        # Start by passing empty argument list
11        # to determine the expected argument count11        # to determine the expected argument count
12        combination = []12        combination = []
13        while True:13        while True:
14            try:14            try:
15                self._lock.pick(*combination)15                self._lock.pick(*combination)
16            except TypeError as ex:16            except TypeError as ex:
17                # This case defines the number of expected arguments17                # This case defines the number of expected arguments
18                if ex.position is None:18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance22                # An unexpected type was passed so pass empty instance
23                # from the expected type23                # from the expected type
24                else:24                else:
25                    combination[ex.position - 1] = ex.expected()25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found26            # An unexpected value of the correct type was found
27            # pass the expected value instead27            # pass the expected value instead
28            except ValueError as ex:28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked30            # No exception means that the lock is unlocked
31            else:31            else:
32                break32                break
3333
34print('This is a test')34print('This is a test')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1class LockPicker_gkunchev:n1class LockPicker_gkunchev1:
2    """Pick a lock. It's that simple!"""2    """Pick a lock. It's that simple!"""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        """Store the lock passed when initializing."""5        """Store the lock passed when initializing."""
6        self._lock = lock6        self._lock = lock
77
8    def unlock(self):8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list10        # Start by passing empty argument list
11        # to determine the expected argument count11        # to determine the expected argument count
12        combination = []12        combination = []
13        while True:13        while True:
14            try:14            try:
15                self._lock.pick(*combination)15                self._lock.pick(*combination)
16            except TypeError as ex:16            except TypeError as ex:
17                # This case defines the number of expected arguments17                # This case defines the number of expected arguments
18                if ex.position is None:18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance22                # An unexpected type was passed so pass empty instance
23                # from the expected type23                # from the expected type
24                else:24                else:
25                    combination[ex.position - 1] = ex.expected()25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found26            # An unexpected value of the correct type was found
27            # pass the expected value instead27            # pass the expected value instead
28            except ValueError as ex:28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked30            # No exception means that the lock is unlocked
31            else:31            else:
32                break32                break
tt33 
34print('This is a test')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1class LockPicker_666:t1class LockPicker_gkunchev:
2    """Pick a lock. It's that simple!"""2    """Pick a lock. It's that simple!"""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        """Store the lock passed when initializing."""5        """Store the lock passed when initializing."""
6        self._lock = lock6        self._lock = lock
77
8    def unlock(self):8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list10        # Start by passing empty argument list
11        # to determine the expected argument count11        # to determine the expected argument count
12        combination = []12        combination = []
13        while True:13        while True:
14            try:14            try:
15                self._lock.pick(*combination)15                self._lock.pick(*combination)
16            except TypeError as ex:16            except TypeError as ex:
17                # This case defines the number of expected arguments17                # This case defines the number of expected arguments
18                if ex.position is None:18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance22                # An unexpected type was passed so pass empty instance
23                # from the expected type23                # from the expected type
24                else:24                else:
25                    combination[ex.position - 1] = ex.expected()25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found26            # An unexpected value of the correct type was found
27            # pass the expected value instead27            # pass the expected value instead
28            except ValueError as ex:28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked30            # No exception means that the lock is unlocked
31            else:31            else:
32                break32                break
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1class LockPicker_gkunchev:t1class LockPicker_666:
2    """Pick a lock. It's that simple!"""2    """Pick a lock. It's that simple!"""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        """Store the lock passed when initializing."""5        """Store the lock passed when initializing."""
6        self._lock = lock6        self._lock = lock
77
8    def unlock(self):8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list10        # Start by passing empty argument list
11        # to determine the expected argument count11        # to determine the expected argument count
12        combination = []12        combination = []
13        while True:13        while True:
14            try:14            try:
15                self._lock.pick(*combination)15                self._lock.pick(*combination)
16            except TypeError as ex:16            except TypeError as ex:
17                # This case defines the number of expected arguments17                # This case defines the number of expected arguments
18                if ex.position is None:18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance22                # An unexpected type was passed so pass empty instance
23                # from the expected type23                # from the expected type
24                else:24                else:
25                    combination[ex.position - 1] = ex.expected()25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found26            # An unexpected value of the correct type was found
27            # pass the expected value instead27            # pass the expected value instead
28            except ValueError as ex:28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked30            # No exception means that the lock is unlocked
31            else:31            else:
32                break32                break
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1class LockPicker_gkunchev:
2    """Pick a lock. It's that simple!"""
13
t2class LockPicker_invalid:t4    def __init__(self, lock):
3    pass5        """Store the lock passed when initializing."""
6        self._lock = lock
7 
8    def unlock(self):
9        """Try to pick the lock. No - there is no try - PICK THE LOCK!"""
10        # Start by passing empty argument list
11        # to determine the expected argument count
12        combination = []
13        while True:
14            try:
15                self._lock.pick(*combination)
16            except TypeError as ex:
17                # This case defines the number of expected arguments
18                if ex.position is None:
19                    # Try with as many None-s as the lock expects as arg count
20                    combination = [None for _ in range(ex.expected)]
21                # This case means the arg count is correct
22                # An unexpected type was passed so pass empty instance
23                # from the expected type
24                else:
25                    combination[ex.position - 1] = ex.expected()
26            # An unexpected value of the correct type was found
27            # pass the expected value instead
28            except ValueError as ex:
29                combination[ex.position - 1] = ex.expected
30            # No exception means that the lock is unlocked
31            else:
32                break
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1f1
t2class NotExpectedName:t2class LockPicker_invalid:
3    pass3    pass
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op