1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 07:02:29 +08:00

Strutil: HammingDistance func (#197)

* Strutil: HammingDistance func

The Hamming distance is the number of positions at which the corresponding symbols are different

* Add hamming distance doc
This commit is contained in:
donutloop
2024-03-03 14:48:34 +01:00
committed by GitHub
parent 3d1bd08434
commit 0b5e884371
5 changed files with 125 additions and 0 deletions

View File

@@ -581,3 +581,26 @@ func TestSubInBetween(t *testing.T) {
assert.Equal("", SubInBetween(str, "a", ""))
assert.Equal("", SubInBetween(str, "a", "f"))
}
func TestHammingDistance(t *testing.T) {
assert := internal.NewAssert(t, "HammingDistance")
hd := func(a, b string) int {
c, _ := HammingDistance(a, b)
return c
}
assert.Equal(0, hd(" ", " "))
assert.Equal(1, hd(" ", "c"))
assert.Equal(1, hd("a", "d"))
assert.Equal(1, hd("a", " "))
assert.Equal(1, hd("a", "f"))
assert.Equal(0, hd("", ""))
assert.Equal(-1, hd("abc", "ab"))
assert.Equal(3, hd("abc", "def"))
assert.Equal(-1, hd("kitten", "sitting"))
assert.Equal(1, hd("ö", "ü"))
assert.Equal(0, hd("日本語", "日本語"))
assert.Equal(3, hd("日本語", "語日本"))
}