Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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 ladders 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 robots 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago