Page 1 of 1

0 ▾ Part 3: A Ladder-climbing Robot (10 points) You have been given a ladder and a small robot that can climb up and dow

Posted: Fri Jun 10, 2022 11:55 am
by correctanswer
0 Part 3 A Ladder Climbing Robot 10 Points You Have Been Given A Ladder And A Small Robot That Can Climb Up And Dow 1
0 Part 3 A Ladder Climbing Robot 10 Points You Have Been Given A Ladder And A Small Robot That Can Climb Up And Dow 1 (104.9 KiB) Viewed 75 times
0 ▾ Part 3: A Ladder-climbing Robot (10 points) You have been given a ladder and a small robot that can climb up and down that ladder, one rung 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 rung up the ladder. If the robot attempts to climb up from the top rung of the ladder, it falls off the ladder and has to start from the bottom. • D (descend) causes the robot to move one rung down the ladder. If the robot is already on the first rung of the ladder, nothing happens (it stays in place). • J (jump) causes the robot to immediately jump off the ladder return to the first rung. Given a size (total number of rungs) for the ladder and a sequence of commands for the robot, your task is to determine which rung of the ladder the robot is standing on at the end of the simulation. Note that the robot always begins on the first rung (rung #1) of the ladder. Complete the function ladder, which takes two arguments: the number of rungs in the ladder and a string of commands, all of which will be provided in uppercase. The function returns the number of the rung the robot ends on. For example, suppose that you have a 4-rung ladder and the 10-instruction sequence 'CDDCCCCCJC'. In this case: 1. c: The robot first moves up one rung to rung 2. 2. D: The robot moves down one rung to rung 1. 3. D: The robot attempts to move down another rung. It is already on the first rung, so nothing happens. 4. c: The robot first moves up one rung to rung 2. 5. c: The robot first moves up one rung to rung 3. 6. c: The robot first moves up one rung to rung 4. 7. c: The robot attempts to move up one rung and falls off the ladder. It starts over at rung 1. 8. c: The robot first moves up one rung to rung 2. 9. J: The robot jumps off the ladder. It starts over at rung 1. 10. c: The robot first moves up one rung to rung 2. 11. The function returns 2. 3