-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhamming.go
More file actions
33 lines (28 loc) · 740 Bytes
/
hamming.go
File metadata and controls
33 lines (28 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package stringosim
import (
"errors"
)
var HAMMING_ERROR_DIFFERENT_LENGTH = errors.New("Can't compare strings of different lengths")
type HammingSimilarityOptions struct {
CaseInsensitive bool
}
var DefaultHammingSimilarityOptions = HammingSimilarityOptions{
CaseInsensitive: false,
}
func Hamming(s []rune, t []rune, options ...HammingSimilarityOptions) (int, error) {
if len(s) != len(t) {
return -1, HAMMING_ERROR_DIFFERENT_LENGTH
}
opt := DefaultHammingSimilarityOptions
for _, option := range options {
opt = option
break
}
ret := 0
for i, cs := range s {
if !SameRune(cs, t[i], opt.CaseInsensitive) {
ret++
}
}
return ret, nil
}