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 86c8a3f

Browse files
author
openset
committed
Add: Power of Four
1 parent b652e5f commit 86c8a3f

File tree

2 files changed

+46
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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