Created
October 22, 2014 02:07
-
-
Save briandowns/f57b5affcef6417bc833 to your computer and use it in GitHub Desktop.
Go Hamming Distance
This file contains 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 main | |
import ( | |
"errors" | |
"log" | |
) | |
func getDistance(s1, s2 string) (int, error) { | |
if len(s1) != len(s2) { | |
return 0, errors.New("ERROR: Strings are of different lengths") | |
} | |
var differences int | |
for i, x := range s1 { | |
if string(x) != string(s2[i]) { | |
differences++ | |
} | |
} | |
return differences, nil | |
} | |
func main() { | |
distance, err := getDistance("GAGCCTACTAACGGGAT", "ATCGTAATGACGGCCT") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
log.Printf("Hamming Distance: %d\n", distance) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment