-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// Return the Hamming distance between the strings, | ||
/// or None if the lengths are mismatched. | ||
pub fn hamming_distance(s1: &str, s2: &str) -> Option<usize> { | ||
if s1.len() != s2.len() { | ||
return None; | ||
} | ||
|
||
if s1.is_empty() || s2 == s1 { | ||
return Some(0); | ||
} | ||
|
||
let mut count = 0; | ||
for (key, ch) in s1.chars().enumerate() { | ||
if s2.as_bytes()[key] != ch as u8 { | ||
count += 1; | ||
} | ||
} | ||
Some(count) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use exercism::hamming; | ||
|
||
fn process_distance_case(strand_pair: [&str; 2], expected_distance: Option<usize>) { | ||
assert_eq!( | ||
hamming::hamming_distance(strand_pair[0], strand_pair[1]), | ||
expected_distance | ||
); | ||
} | ||
#[test] | ||
fn empty_strands() { | ||
process_distance_case(["", ""], Some(0)); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// disallow first strand longer | ||
fn disallow_first_strand_longer() { | ||
process_distance_case(["AATG", "AAA"], None); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// disallow second strand longer | ||
fn disallow_second_strand_longer() { | ||
process_distance_case(["ATA", "AGTG"], None); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn first_string_is_longer() { | ||
process_distance_case(["AAA", "AA"], None); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn second_string_is_longer() { | ||
process_distance_case(["A", "AA"], None); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// single letter identical strands | ||
fn single_letter_identical_strands() { | ||
process_distance_case(["A", "A"], Some(0)); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// small distance | ||
fn single_letter_different_strands() { | ||
process_distance_case(["G", "T"], Some(1)); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// long identical strands | ||
fn long_identical_strands() { | ||
process_distance_case(["GGACTGAAATCTG", "GGACTGAAATCTG"], Some(0)); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn no_difference_between_identical_strands() { | ||
process_distance_case(["GGACTGA", "GGACTGA"], Some(0)); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn complete_hamming_distance_in_small_strand() { | ||
process_distance_case(["ACT", "GGA"], Some(3)); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn small_hamming_distance_in_the_middle_somewhere() { | ||
process_distance_case(["GGACG", "GGTCG"], Some(1)); | ||
} | ||
#[test] | ||
#[ignore] | ||
fn larger_distance() { | ||
process_distance_case(["ACCAGGG", "ACTATGG"], Some(2)); | ||
} | ||
#[test] | ||
#[ignore] | ||
/// large distance in off-by-one strand | ||
fn long_different_strands() { | ||
process_distance_case(["GGACGGATTCTG", "AGGACGGATTCT"], Some(9)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ mod binary_search; | |
mod spiral_matrix; | ||
mod allergies; | ||
mod nucleotide_count; | ||
mod hamming; |