Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d3d5a1

Browse files
author
Shuo
authored
Merge pull request #531 from openset/develop
Add: Robot Bounded In Circle
2 parents b08bba9 + 2ba89a3 commit 0d3d5a1

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package robot_bounded_in_circle
2+
3+
func isRobotBounded(instructions string) bool {
4+
x, y, dx, dy := 0, 0, 0, 1
5+
for _, i := range instructions {
6+
switch i {
7+
case 'G':
8+
x, y = x+dx, y+dy
9+
case 'L':
10+
dx, dy = -dy, dx
11+
case 'R':
12+
dx, dy = dy, -dx
13+
}
14+
}
15+
return x == 0 && y == 0 || dx != 0 || dy != 1
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package robot_bounded_in_circle
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected bool
8+
}
9+
10+
func TestIsRobotBounded(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "GGLLGG",
14+
expected: true,
15+
},
16+
{
17+
input: "GG",
18+
expected: false,
19+
},
20+
{
21+
input: "GL",
22+
expected: true,
23+
},
24+
{
25+
input: "GGLLGGGGRRGG",
26+
expected: true,
27+
},
28+
{
29+
input: "GGRRGG",
30+
expected: true,
31+
},
32+
{
33+
input: "GLRLLGLL",
34+
expected: true,
35+
},
36+
{
37+
input: "GLGRGLGLGLGL",
38+
expected: false,
39+
},
40+
}
41+
for _, tc := range tests {
42+
output := isRobotBounded(tc.input)
43+
if output != tc.expected {
44+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
45+
}
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /