Problem
ProblemFigure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colored red.Given two strings s and t of equal length, the Hamming distance between s and t, denoted dH(s,t), is the number of corresponding symbols that differ in s and t. See Figure 2.Given: Two DNA strings s and t of equal length (not exceeding 1 kbp).Return: The Hamming distance dH(s,t).
汉明距离计算

问题:input: 对于给定的一对sequence(长度相等)output: 两条序列的汉明距离
# inputGAGCCTACTAACGGGATCATCGTAATGACGGCCT# output7
solution -1
def hm_dis(s1, s2):return sum(e1 != e2 for e1, e2 in zip(s1, s2))def main():s1 = "GAGCCTACTAACGGGAT"s2 = "CATCGTAATGACGGCCT"print(hm_dis(s1, s2))if __name__ == '__main__':main()
solution-2
由于工作的时候会用到,且需要快速运行,补一个cython版本
cpdef int hm_dis(str s1, str s2):cdef str e1cdef str e2cdef int dis = 0cdef int loc = 0for e1 in s1:e2 = s2[loc]loc = loc + 1if e1 != e2:dis += 1#end-if#end-forreturn dis
solution-3
Golang版本
package mainimport ("fmt")//func hm(s1_p, s2_p *string) int {s1, s2 := *s1_p, *s2_pif len(s1) != len(s2){panic("length error")}dis := 0for i := 0; i < len(s1); i++ {if s1[i] != s2[i] {dis++}}return dis}func main() {seq1 := "GAGCCTACTAACGGGAT"seq2 := "CATCGTAATGACGGCCT"d := hm(&seq1, &seq2)fmt.Println(d)}
