| 1 |
package smetrics |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
// The Hamming distance is the minimum number of substitutions required to change string A into string B. Both strings must have the same size. If the strings have different sizes, the function returns an error. |
| 8 |
func Hamming(a, b string) (int, error) { |
| 9 |
al := len(a) |
| 10 |
bl := len(b) |
| 11 |
|
| 12 |
if al != bl { |
| 13 |
return -1, fmt.Errorf("strings are not equal (len(a)=%d, len(b)=%d)", al, bl) |
| 14 |
} |
| 15 |
|
| 16 |
var difference = 0 |
| 17 |
|
| 18 |
for i := range a { |
| 19 |
if a[i] != b[i] { |
| 20 |
difference = difference + 1 |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return difference, nil |
| 25 |
} |