PYTHON CODING HELP!
Posted: Wed Apr 27, 2022 3:32 pm
PYTHON CODING HELP!
Your task is to write a function second_lock (gems) that determines whether, for a given configuration of gems in the source branch, it is possible to unlock the door. Your function should take a list of integer gem numbers as input (in which the gem at position o in the list is the last gem that can be moved from the source branch). Your function should return a tuple consisting of a boolean value specifying whether the door can be unlocked or not, and an integer corresponding to the largest number of gems that are required to be located on the store branch at any one point in time while the puzzle is being solved. If the door cannot be unlocked, this number should be - 1.
Assumptions: • You may assume that the N gems are numbered from 1..N inclusive, with no missing or repeated numbers. • You may assume that all branches are sufficiently long to contain all of the gems (ie, you won't run out of space on any branch). Example calls: >>> print(second_lock ( [4, 3, 2, 1])) (True, 0) >>> print (second_lock ([1, 2, 3, 4, 5])) (True, 4) >>> print (second_lock ([6, 1, 4, 5, 2, 3])). (False, -1) >>> print (second_lock ([3, 4, 5, 2, 1])) (True, 2)
Explanation: In the first example, the initial configuration of the gems on the source branch is correctly ordered, so they can be slide directly onto the destination branch. In the second example, the initial configuration of the gems on the source branch is in reverse order, so the first four gems must be slid onto the store branch, then the gem numbered i can slid onto the destination branch, followed by the remaining gems from the store branch, which will now be correctly ordered. In the third example, it is not possible to correctly configure the gems as, after the first four gems have been slid onto the store branch (so that the gem numbered 1 can be slid onto the destination branch), the accessible gem on the store branch (and hence the only gem which can be moved) is numbered 4. Recall | that gems, once slid onto the store or destination branches cannot be slid back onto the source branch. The fourth example can be visualised as follows:
Step 2: Gem 3 needs to slide into the destination next; however, it is blocked by gems 5 and 4. These two gems need to slide into the store branch: 1-3 --2-1-1 4 5 Step 3: Gem 3 can then slide directly into the destination: --3-2-1-1 4 5 |
Step 4: Gems 4 and 5 can now slide out of the store and into the destination. This lock can be opened and the maximum number of gems on the store branch at any one time was 2: -5-4-3-2-1-1 |
Your task is to write a function second_lock (gems) that determines whether, for a given configuration of gems in the source branch, it is possible to unlock the door. Your function should take a list of integer gem numbers as input (in which the gem at position o in the list is the last gem that can be moved from the source branch). Your function should return a tuple consisting of a boolean value specifying whether the door can be unlocked or not, and an integer corresponding to the largest number of gems that are required to be located on the store branch at any one point in time while the puzzle is being solved. If the door cannot be unlocked, this number should be - 1.
Assumptions: • You may assume that the N gems are numbered from 1..N inclusive, with no missing or repeated numbers. • You may assume that all branches are sufficiently long to contain all of the gems (ie, you won't run out of space on any branch). Example calls: >>> print(second_lock ( [4, 3, 2, 1])) (True, 0) >>> print (second_lock ([1, 2, 3, 4, 5])) (True, 4) >>> print (second_lock ([6, 1, 4, 5, 2, 3])). (False, -1) >>> print (second_lock ([3, 4, 5, 2, 1])) (True, 2)
Explanation: In the first example, the initial configuration of the gems on the source branch is correctly ordered, so they can be slide directly onto the destination branch. In the second example, the initial configuration of the gems on the source branch is in reverse order, so the first four gems must be slid onto the store branch, then the gem numbered i can slid onto the destination branch, followed by the remaining gems from the store branch, which will now be correctly ordered. In the third example, it is not possible to correctly configure the gems as, after the first four gems have been slid onto the store branch (so that the gem numbered 1 can be slid onto the destination branch), the accessible gem on the store branch (and hence the only gem which can be moved) is numbered 4. Recall | that gems, once slid onto the store or destination branches cannot be slid back onto the source branch. The fourth example can be visualised as follows:
Step 2: Gem 3 needs to slide into the destination next; however, it is blocked by gems 5 and 4. These two gems need to slide into the store branch: 1-3 --2-1-1 4 5 Step 3: Gem 3 can then slide directly into the destination: --3-2-1-1 4 5 |
Step 4: Gems 4 and 5 can now slide out of the store and into the destination. This lock can be opened and the maximum number of gems on the store branch at any one time was 2: -5-4-3-2-1-1 |