Skip to content

Commit 1e3fad6

Browse files
author
Shuo
authored
Merge pull request #389 from openset/develop
Add: Power of Two
2 parents 237436d + ae5c12a commit 1e3fad6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
package power_of_two
2+
3+
func isPowerOfTwo(n int) bool {
4+
return n > 0 && n&(n-1) == 0
5+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package power_of_two
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected bool
8+
}
9+
10+
func TestIsPowerOfTwo(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 0,
14+
expected: false,
15+
},
16+
{
17+
input: 1,
18+
expected: true,
19+
},
20+
{
21+
input: 12,
22+
expected: false,
23+
},
24+
{
25+
input: 16,
26+
expected: true,
27+
},
28+
{
29+
input: 218,
30+
expected: false,
31+
},
32+
}
33+
for _, tc := range tests {
34+
output := isPowerOfTwo(tc.input)
35+
if output != tc.expected {
36+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)