Skip to content

Commit 8a59509

Browse files
authored
Merge pull request thuva4#313 from stripedpajamas/GoHammingDistance
HammingDistance in Golang
2 parents fecd20f + df53edc commit 8a59509

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
138138
- [d-grossman](https://github.com/d-grossman)
139139
- [javmonisu](https://github.com/javmonisu)
140140
- [lena15n](https://github.com/lena15n)
141+
- [stripedpajamas](https://github.com/stripedpajamas)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package hammingDistance
2+
3+
func HammingDistance(a, b string) int {
4+
if len(a) != len(b) {
5+
panic("The two strings must have equal length")
6+
}
7+
aRunes := []rune(a)
8+
bRunes := []rune(b)
9+
distance := 0
10+
for i, r := range aRunes {
11+
if r != bRunes[i] {
12+
distance++
13+
}
14+
}
15+
return distance
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package hammingDistance
2+
3+
import "testing"
4+
5+
func TestHammingDistance(t *testing.T) {
6+
// "karolin" => "kathrin" is 3 according to Wikipedia
7+
if HammingDistance("karolin", "kathrin") != 3 {
8+
t.Fail()
9+
}
10+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ FisherYatesShuffle | :+1: | | | | :+1: | :+1: | | :+1: |
2929
FloodFill Algorithm | :+1: | | | | | | | |
3030
Floyd'sAlgorithm | :+1: | :+1: | | | :+1: | | | |
3131
GreatestCommonDivisor | :+1: | | :+1: | :+1: | :+1: | | | |
32-
HammingDistance | :+1: | :+1: | | :+1: | | :+1: | | |
32+
HammingDistance | :+1: | :+1: | | :+1: | | :+1: | :+1: | |
3333
HeapSort | :+1: | :+1: | | | :+1: | :+1: | :+1: | | :+1:
3434
HistogramEqualization | :+1: | | | | | | | |
3535
InsertionSort | :+1: | :+1: | :+1: | :+1: | :+1: | | :+1: | :+1: |

0 commit comments

Comments
 (0)