Editor

Robot on a Grid

#46 · Simulation · Beginner · 40 XP · about 8 min

A robot stands at (0, 0) on a grid, facing north, the direction in which y grows. It understands three commands:

  • F: move forward one square in the direction it is facing
  • L: turn 90 degrees to the left, staying on the same square
  • R: turn 90 degrees to the right, staying on the same square

Write robot_walk(commands) that follows the string commands from left to right and returns the robot's final position as a tuple (x, y). East is the direction in which x grows.

Examples

robot_walk('FF')      # returns (0, 2)
robot_walk('FRFF')    # returns (2, 1)
robot_walk('LLFF')    # returns (0, -2)
robot_walk('')        # returns (0, 0)

In 'FRFF', the robot steps north to (0, 1), turns to face east, then takes two steps to (2, 1).

Note

commands contains only the letters F, L and R, and can be up to 10,000 long.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.