Robot Bounded In Circle
Real-world version: Does the Rover Stay Near HomeThe statement here is our own: the same inputs, outputs and technique.
A rover starts at the origin facing north and follows instructions, one character at a time: 'G' moves it one step forward in the direction it faces, 'L' turns it a quarter turn to the left, and 'R' a quarter turn to the right.
The rover repeats the whole list of instructions over and over, without end. Return true when there is some circle around the origin that the rover never leaves.
Examples
Example 1
- Input
- instructions = "GGLRGGRL"
- Output
- false
A left turn and a right turn cancel each other out, so the rover ends four steps north still facing north, and every repeat carries it four further.
Example 2
- Input
- instructions = "GLGLGLGL"
- Output
- true
The rover traces a unit square and comes back to where it started facing north, so it goes round that square forever.
Example 3
- Input
- instructions = "GGRGGR"
- Output
- true
The rover ends two steps north and two east facing south. Repeating turns the whole journey a half turn each time, so the two halves cancel and it stays near home.
Constraints
- 1 <= instructions.length <= 100
- instructions[i] is 'G', 'L' or 'R'
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def is_robot_bounded(instructions: str) -> bool:public boolean isRobotBounded(String instructions)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's