Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part III: Climbing The Ladder (4 points) You have been given a stepladder and a

ID: 3751363 • Letter: P

Question

Part III: Climbing The Ladder (4 points)

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:
C (climb) causes the robot to move one step up the ladder. If the robot attempts to climb up when it is already on 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 where it is on step 1).
R (reset) causes the robot to immediately return to step 1 of the ladder (this counts as a single operation).

The ladder() function takes two arguments: a positive (non-zero) integer representing the ladder’s size (total number of
steps) and a string representing a sequence of commands. This function returns the total number of instructions that will be
executed before the robot falls to its doom from the top of the ladder (this count includes the final, fatal instruction). You may
assume that every instruction sequence eventually leads to the destruction of the unfortunate robot. You may also assume
that every instruction sequence only contains the command characters listed above.
The robot always begins on 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 performing 10 instructions. Any remaining commands are ignored.

Hints:
Your general solution structure should be a for loop containing one or more conditional (if, elif, etc.) statements.
Use extra variables to track the robot’s current step on the ladder and the total number of instructions performed so far.
If (technically when) the robot is destroyed, use a return statement inside your loop to break out of the function
immediately and send back the final instruction count.


Examples:
Function Call Return Value
ladder(1, "C") 1
ladder(1, "DDRCDCC") 4
ladder(4, "CDDCCCCCRCCCCDDDD") 7

Explanation / Answer

Since there is no language constraint I would like to give the solution in python for simplicity. If you need your solution in another language you can always comment on this answer.

Python code: