Skip to content

Commit 144e988

Browse files
committed
solved problem 461 Hamming Distance
1 parent d49b03c commit 144e988

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

hamming_distance/hamming_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package hamming_distance
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type TestCase struct {
9+
id int
10+
description string
11+
input Input
12+
expectedResult int
13+
}
14+
15+
type Input struct {
16+
a int
17+
b int
18+
}
19+
20+
type TestSuite []TestCase
21+
22+
var ts = TestSuite{
23+
TestCase{
24+
id: 1,
25+
description: "test case from task - different numbers",
26+
input: Input{
27+
a: 1,
28+
b: 4,
29+
},
30+
expectedResult: 2,
31+
},
32+
TestCase{
33+
id: 2,
34+
description: "Diggerent length of binary",
35+
input: Input{
36+
a: 1,
37+
b: 136,
38+
},
39+
expectedResult: 3,
40+
},
41+
TestCase{
42+
id: 3,
43+
description: "Same numbers",
44+
input: Input{
45+
a: 136,
46+
b: 136,
47+
},
48+
expectedResult: 0,
49+
},
50+
}
51+
52+
func TestHamming(t *testing.T) {
53+
54+
for _, tc := range ts {
55+
t.Run(fmt.Sprintf("Test%d", tc.id), func(t *testing.T) {
56+
57+
actualResult := hammingDistance(tc.input.a, tc.input.b)
58+
59+
if actualResult != tc.expectedResult {
60+
fmt.Printf(" - Got: %d\n - Expected: %d\n", actualResult, tc.expectedResult)
61+
t.Fail()
62+
}
63+
64+
})
65+
}
66+
67+
}

hamming_distance/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package hamming_distance
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
func hammingDistance(x int, y int) int {
9+
var result int
10+
binX := strconv.FormatInt(int64(x), 2)
11+
binY := strconv.FormatInt(int64(y), 2)
12+
13+
fmt.Printf("Before diff fix:\n - X: %s\n - Y: %s\n", binX, binY)
14+
var diffLen int
15+
16+
switch {
17+
case len(binX) < len(binY):
18+
diffLen = len(binY) - len(binX)
19+
binX = addLeadZeros(binX, diffLen)
20+
21+
case len(binX) > len(binY):
22+
diffLen = len(binX) - len(binY)
23+
binY = addLeadZeros(binY, diffLen)
24+
}
25+
26+
fmt.Printf("After diff fix\n - X: %s\n - Y: %s\n", binX, binY)
27+
for index, digit := range binX {
28+
if string([]rune(binY)[index]) != string(digit) {
29+
30+
result++
31+
32+
}
33+
}
34+
35+
return result
36+
}
37+
38+
func addLeadZeros(bin string, diff int) string {
39+
//fmt.Printf("Diff is %d\n", diff)
40+
//fmt.Printf("string is %s\n", bin)
41+
42+
var tmp string
43+
for i := 0; i < diff; i++ {
44+
tmp += "0"
45+
//fmt.Printf("Step[%d]: tmp is [%s]\n", i, tmp)
46+
}
47+
bin = tmp + bin
48+
return bin
49+
}

hamming_distance/spec.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
461 Hamming Distance
2+
https://leetcode.com/problems/hamming-distance/description/
3+
4+
5+
6+
7+
The Hamming distance between two integers is the number of positions
8+
at which the corresponding bits are different.
9+
10+
Given two integers x and y, calculate the Hamming distance.
11+
12+
Note:
13+
`0 ≤ x, y < 231.`
14+
15+
Example:
16+
```
17+
Input: x = 1, y = 4
18+
19+
Output: 2
20+
21+
Explanation:
22+
1 (0 0 0 1)
23+
4 (0 1 0 0)
24+
↑ ↑
25+
```
26+
27+
28+
The above arrows point to positions where the corresponding bits are different.

0 commit comments

Comments
 (0)