Heading walker
EasyOnline Assessment
Follow turn-and-move commands and report where the walker ends up.
The walker starts at [0, 0] facing north. Moving north increases y by 1 and moving east increases x by 1 (so south decreases y and west decreases x). Process commands one character at a time:
L: turn 90 degrees left, staying in place.R: turn 90 degrees right, staying in place.F: move one unit forward in the current direction.
Input: commands, a string made only of the characters L, R and F.
Output: the final position as a list [x, y]. An empty string returns [0, 0].
Example 1
- Input:
- ["FFRFLF"]
- Output:
- [1,3]
- Explanation
- Two steps north reach (0, 2). Turn right to face east, step to (1, 2), turn left to face north again, and step to (1, 3).
Example 2
- Input:
- ["RRFF"]
- Output:
- [0,-2]
- Explanation
- Turning right twice makes the walker face south, so two steps forward end at (0, -2).
Example 3
- Input:
- ["FRFRFRF"]
- Output:
- [0,0]
- Explanation
- The walker traces a square: north to (0, 1), east to (1, 1), south to (1, 0), west to (0, 0). It ends back at the origin.
Constraints
0 <= commands.length <= 10,000commandscontains only the charactersL,RandF