Last active
February 15, 2021 19:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment