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 6fbcedc

Browse files
committed
Add: Hamming Distance
1 parent 66ef384 commit 6fbcedc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
package hamming_distance
2+
3+
func hammingDistance(x int, y int) int {
4+
ans, n := 0, x^y
5+
for n > 0 {
6+
ans, n = ans+1, n&(n-1)
7+
}
8+
return ans
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package hamming_distance
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
x int
7+
y int
8+
expected int
9+
}
10+
11+
func TestHammingDistance(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
x: 1,
15+
y: 4,
16+
expected: 2,
17+
},
18+
}
19+
for _, tc := range tests {
20+
output := hammingDistance(tc.x, tc.y)
21+
if output != tc.expected {
22+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.x, tc.y, output, tc.expected)
23+
}
24+
}
25+
}

0 commit comments

Comments
(0)

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