Skip to content

Commit

Permalink
Strutil: HammingDistance func (#197)
Browse files Browse the repository at this point in the history
* Strutil: HammingDistance func

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

* Add hamming distance doc
  • Loading branch information
donutloop committed Mar 3, 2024
1 parent 3d1bd08 commit 0b5e884
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/api/packages/strutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
- [ContainsAny](#ContainsAny)
- [RemoveWhiteSpace](#RemoveWhiteSpace)
- [SubInBetween](#SubInBetween)
- [HammingDistance](#HammingDistance)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -1495,4 +1496,36 @@ func main() {
// abc
// bc
}
```

### <span id="HammingDistance">HammingDistance</span>

<p>TBD</p>

<b>函数签名:</b>

```go
HammingDistance(a, b string) (int, error)
```

<b>示例:</b>

```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)

func main() {

result1, _ := strutil.HammingDistance("de", "de")
result2, _ := strutil.HammingDistance("a", "d")

fmt.Println(result1)
fmt.Println(result2)

// Output:
// 0
// 1
}
```
34 changes: 34 additions & 0 deletions docs/en/api/packages/strutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import (
- [ContainsAll](#ContainsAll)
- [ContainsAny](#ContainsAny)
- [RemoveWhiteSpace](#RemoveWhiteSpace)
- [SubInBetween](#SubInBetween)
- [HammingDistance](#HammingDistance)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -1496,4 +1498,36 @@ func main() {
// abc
// bc
}
```

### <span id="HammingDistance">HammingDistance</span>

<p>HammingDistance calculates the Hamming distance between two strings. The Hamming distance is the number of positions at which the corresponding symbols are different</p>

<b>Signature:</b>

```go
HammingDistance(a, b string) (int, error)
```

<b>Example:</b>

```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)

func main() {

result1, _ := strutil.HammingDistance("de", "de")
result2, _ := strutil.HammingDistance("a", "d")

fmt.Println(result1)
fmt.Println(result2)

// Output:
// 0
// 1
}
```
22 changes: 22 additions & 0 deletions strutil/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package strutil

import (
"errors"
"regexp"
"strings"
"unicode"
Expand Down Expand Up @@ -594,3 +595,24 @@ func SubInBetween(str string, start string, end string) string {

return ""
}

// HammingDistance calculates the Hamming distance between two strings.
// The Hamming distance is the number of positions at which the corresponding symbols are different.
// This func returns an error if the input strings are of unequal lengths.
func HammingDistance(a, b string) (int, error) {
if len(a) != len(b) {
return -1, errors.New("a length and b length are unequal")
}

ar := []rune(a)
br := []rune(b)

var distance int
for i, codepoint := range ar {
if codepoint != br[i] {
distance++
}
}

return distance, nil
}
13 changes: 13 additions & 0 deletions strutil/string_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,16 @@ func ExampleSubInBetween() {
// abc
// bc
}

func ExampleHammingDistance() {

result, _ := HammingDistance("abc", "def")
fmt.Println(result)

result, _ = HammingDistance("name", "namf")
fmt.Println(result)

// Output:
// 3
// 1
}
23 changes: 23 additions & 0 deletions strutil/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("日本語", "語日本"))
}

0 comments on commit 0b5e884

Please sign in to comment.