Hamming

1. Readme

汉明(Hamming)

给 2 个长度为 n 的 DNA 序列,求汉明距离差异为多少。

通过比较两条 DNA 链,并计算其中有多少核苷酸与其他序列中的同等核苷酸不同.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^

这两条 DNA 链之间的 汉明距离为 7.

实现说明

汉明长度仅定义为等长的序列,因此尝试计算不同长度的序列之间的汉明 距离不应该起作用。这种情况的一般处理(例如,引发异常与返回特殊值)可能在语言之间有所不同.

资源

罗瑟琳的计算点突变问题http://rosalind.info/problems/hamm/

2. 开始你的表演

/// Return the Hamming distance between the strings,
/// or None if the lengths are mismatched.
pub fn hamming_distance(s1: &str, s2: &str) -> Option<usize> {
   unimplemented!("What is the Hamming Distance between {:?} and {:?}", s1, s2);
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[test]
fn test_no_difference_between_empty_strands() {
   assert_eq!(hamming_distance("", ""), Some(0));
}

#[test]
//#[ignore]
fn test_no_difference_between_identical_strands() {
   assert_eq!(hamming_distance("GGACTGA", "GGACTGA"), Some(0));
}

#[test]
//#[ignore]
fn test_complete_hamming_distance_in_small_strand() {
   assert_eq!(hamming_distance("ACT", "GGA"), Some(3));
}

#[test]
//#[ignore]
fn test_small_hamming_distance_in_the_middle_somewhere() {
   assert_eq!(hamming_distance("GGACG", "GGTCG"), Some(1));
}

#[test]
//#[ignore]
fn test_larger_distance() {
   assert_eq!(hamming_distance("ACCAGGG", "ACTATGG"), Some(2));
}

#[test]
//#[ignore]
fn test_first_string_is_longer() {
   assert_eq!(hamming_distance("AAA", "AA"), None);
}

#[test]
//#[ignore]
fn test_second_string_is_longer() {
   assert_eq!(hamming_distance("A", "AA"), None);
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
pub fn hamming_distance(a: &str, b: &str) -> Option<usize> {
   if a.len() != b.len() {
       return None;
   }

   Some(a.chars().zip(b.chars()).filter(|&(a, b)| a != b).count())
}

#}



填充/相关