File tree Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments