Question: You have been given a stepladder and a small robot that can climb up and down that ladder, one step at a time. The robot

You have been given a stepladder and a small robot that can climb up and down that ladder, one step at a time. The robot can be programmed using a language that contains exactly three commands, which are always given using uppercase letters: C (climb) causes the robot to move one step up the ladder. If the robot attempts to climb up from the top step of the ladder, it falls off the ladder and is destroyed. D (descend) causes the robot to move one step down the ladder. If the robot is already on the first step of the ladder, nothing happens (it stays in place). R (reset) causes the robot to immediately return to the first step of the ladder. Given a size (total number of steps) for the ladder and a sequence of commands for the robot, your task is to determine how many instructions will be executed before the robot falls to its doom from the top of the ladder. You may assume that every instruction sequence always leads to the destruction of the unfortunate robot. You may also assume that every instruction sequence only contains the three commands listed above. Put your solution into the ladder() function that we have provided for you. The robot always begins on the first step (step 1) of the ladder. For example, suppose that you have a 4-step ladder and the instruction sequence CDDCCRCCCCCCCDDDD. In this case:

1. The robot first moves up one step to step 2.

2. The robot moves down one step to step 1.

3. The robot attempts to move down another step. It is already on the first step, so nothing happens.

4. The robot moves up one step to step 2.

5. The robot moves up one step to step 3.

6. The robot resets and moves back to step 1.

7. The robot moves up one step to step 2.

8. The robot moves up one step to step 3.

9. The robot moves up one step to step 4. Note that this is the top step of the ladder.

10. The robot attempts to move up one step and falls off the ladder.

Thus, the robot self-destructs after 10 instructions. The rest of the instruction sequence is ignored.

def ladder(steps, commands): # Add your code here. You may also need to change the line below.  return -1 
############### Part 3 Tests ############### print('Testing ladder() for steps = 4 and commands = "CDDCCCCCRCCCCDDDD": ' + str(ladder(4, "CDDCCCCCRCCCCDDDD"))) print('Testing ladder() for steps = 1 and commands = "DDRCDCC": ' + str(ladder(1, "DDRCDCC"))) print('Testing ladder() for steps = 1 and commands = "C": ' + str(ladder(1, "C"))) # Write your own tests for Part 3 here! 

ANSWER

Function Call Return Value

ladder(4, "CDDCCCCCRCCCCDDDD") 7

ladder(1, "DDRCDCC") 4

ladder(1, "C") 1

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!