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 ae5c12a

Browse files
author
openset
committed
Add: Power of Two
1 parent 86c8a3f commit ae5c12a

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)

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