Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active February 15, 2021 19:49
Show Gist options
  • Select an option

  • Save sheldonhull/479dcd0fc08a7b67cc9bb3a3f022f0ad to your computer and use it in GitHub Desktop.

Select an option

Save sheldonhull/479dcd0fc08a7b67cc9bb3a3f022f0ad to your computer and use it in GitHub Desktop.
[exercism.io hamming distance] Solving Hamming distance problem on exercism.io #golang #puzzles #algorithms
package hamming
import (
"errors"
"strings"
)
func Distance(a, b string) (int, error) {
if a == b {
return 0, nil
}
if len(a) != len(b) {
return 0, errors.New("Sequence lengths are different")
}
diffCount := 0
aString := strings.Split(a, "")
bString := strings.Split(b, "")
for i, x := range aString {
if x != bString[i] {
diffCount++
}
}
return diffCount, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment