An astronaut sent out a robot to move around on planet mu. However, the robot went missing and the la astronaut could retrieve is its movement record. The astronaut knows that a robot can only move forward backward. Fortunately, there is no error in the retrieved record, so it collects every movement from when started moving.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Python Language - Robot Location

An astronaut sent out a robot to move around on planet mu. However, the robot went missing and the last thing the
astronaut could retrieve is its movement record. The astronaut knows that a robot can only move forward or
backward. Fortunately, there is no error in the retrieved record, so it collects every movement from when the robot
started moving.
The movement record is actually a string consists of multiple occurrences of some positive integer N followed by
either characters F or B indicating if a robot moved forward or backward for N meters. That is, by following the
robot movement record, the robot could be back at the origin, some distance ahead of its origination, or some
distance behind its origination.
Your task is to write a function robot_location(record:str) -> str that takes the robot movement record string
record and returns a string indicating the location of the robot. If the robot is ahead of its origination, we denote it
with + followed by how far in meters from its origination. If the robot is behind of its origination, we denote with
followed by how far in meters from its origination. Otherwise, if the robot is back at its origination, then the function
should return a string 'o'.
For example, the record 1F10F2B means that the robot move forward for 1 meter, then move forward for 10 meters,
and then move backward for 2 meters. That is, the robot is now at 9 meters ahead from its origin. Hence, the function
shall return a string '+9' .
Hint: each character of record is either F
B or a digit.
Examples
robot_location('')
'0'
# the robot doesn't move at all
==
robot_location('1F')
'+1'
# the robot move forward for 1 metre
==
robot_location('1B')
-1'
# the robot move backward for 1 metre
==
robot_location('100F')
'+100'
==
robot_location('1B1F')
'
==
robot_location('2B1F')
'-1'
==
robot_location('1F2B3F')
'+2'
==
robot_location('1F1F3F')
'+5'
==
robot_location('1B1B1B')
-3'
robot_location('1B2F10B')
'-9'
==
robot_location('100F99B')
'+1'
==
robot_location('1F2B3F4B')
robot_location('5F3B1F10F')
'-2'
==
'+13
==
Transcribed Image Text:An astronaut sent out a robot to move around on planet mu. However, the robot went missing and the last thing the astronaut could retrieve is its movement record. The astronaut knows that a robot can only move forward or backward. Fortunately, there is no error in the retrieved record, so it collects every movement from when the robot started moving. The movement record is actually a string consists of multiple occurrences of some positive integer N followed by either characters F or B indicating if a robot moved forward or backward for N meters. That is, by following the robot movement record, the robot could be back at the origin, some distance ahead of its origination, or some distance behind its origination. Your task is to write a function robot_location(record:str) -> str that takes the robot movement record string record and returns a string indicating the location of the robot. If the robot is ahead of its origination, we denote it with + followed by how far in meters from its origination. If the robot is behind of its origination, we denote with followed by how far in meters from its origination. Otherwise, if the robot is back at its origination, then the function should return a string 'o'. For example, the record 1F10F2B means that the robot move forward for 1 meter, then move forward for 10 meters, and then move backward for 2 meters. That is, the robot is now at 9 meters ahead from its origin. Hence, the function shall return a string '+9' . Hint: each character of record is either F B or a digit. Examples robot_location('') '0' # the robot doesn't move at all == robot_location('1F') '+1' # the robot move forward for 1 metre == robot_location('1B') -1' # the robot move backward for 1 metre == robot_location('100F') '+100' == robot_location('1B1F') ' == robot_location('2B1F') '-1' == robot_location('1F2B3F') '+2' == robot_location('1F1F3F') '+5' == robot_location('1B1B1B') -3' robot_location('1B2F10B') '-9' == robot_location('100F99B') '+1' == robot_location('1F2B3F4B') robot_location('5F3B1F10F') '-2' == '+13 ==
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY