Skip to content

Commit af6d362

Browse files
committed
Robot starting location
1 parent 5ce0afb commit af6d362

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ There might still be some personal comments added for my learning purposes.
4646
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/integer-to-roman/integer-to-roman.test.js)
4747

4848
- **All**
49+
* Robot Return to Origin
50+
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/robot/robot.js)
51+
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/robot/robot.test.js)
4952
* Snail
5053
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.js)
5154
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.test.js)

src/robot/robot.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Problem: The robot moves on an plane, and its movements are described by a command string consisting of one or more of the following letters.
2+
// - G instructs the robot to move forward one step
3+
// - L instructs the robot to turn left
4+
// - R instructs the robot to turn right
5+
// The robot cannot go backwards. After running all of the movement commands, check if robot returns to the starting location.
6+
// Time complexity: O(n)
7+
8+
function checkRobotLocation(str) {
9+
const operation = str.split("");
10+
let x = 0;
11+
let y = 0;
12+
let directions = ["N", "E", "S", "W"];
13+
let direction = directions[0];
14+
15+
operation.forEach((task) => {
16+
if (task === "G") {
17+
direction === "N" && (y += 1);
18+
direction === "S" && (y -= 1);
19+
direction === "E" && (x += 1);
20+
direction === "W" && (x -= 1);
21+
} else if (task === "R") {
22+
let i = directions.indexOf(direction);
23+
directions.length <= i + 1
24+
? (direction = directions[0])
25+
: (direction = directions[i + 1]);
26+
} else if (task === "L") {
27+
let i = directions.indexOf(direction);
28+
i > 0
29+
? (direction = directions[i - 1])
30+
: (direction = directions[directions.length - 1]);
31+
}
32+
});
33+
return x === 0 && y === 0 ? true : false;
34+
}
35+
36+
export default checkRobotLocation;

src/robot/robot.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import checkRobotLocation from "./robot";
2+
3+
describe("Check if robot comes back to the original location", () => {
4+
test("Check if robot comes back to the original location", () => {
5+
expect(checkRobotLocation("GRGRGRG")).toBe(true);
6+
});
7+
test("Check if robot comes back to the original location", () => {
8+
expect(checkRobotLocation("GRGLGLGLGG")).toBe(true);
9+
});
10+
test("Check if robot comes back to the original location", () => {
11+
expect(checkRobotLocation("GRGLGGGLGLGG")).toBe(false);
12+
});
13+
});

0 commit comments

Comments
 (0)