Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions exercises/nucleotide-count/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use std::collections::HashMap;
static VALID_NUCLEOTIDES: &'static str = "ACGT";

pub fn count(nucleotide: char, input: &str) -> Result<usize, char> {
let valid = |x: char| { VALID_NUCLEOTIDES.contains(x) };
if valid(nucleotide) && input.chars().all(valid) {
Ok(input.chars().filter(|&c| c == nucleotide).count())
} else {
let valid = |x: char| VALID_NUCLEOTIDES.contains(x);

if !valid(nucleotide) {
Err(nucleotide)
} else {
match input.chars().find(|&c| !valid(c)) {
Some(c) => Err(c),
None => Ok(input.chars().filter(|&c| c == nucleotide).count()),
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions exercises/nucleotide-count/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
use std::collections::HashMap;

pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
unimplemented!(
"How much of nucleotide type '{}' is contained inside DNA string '{}'?",
nucleotide,
dna
);
}

pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
unimplemented!(
"How much of every nucleotide type is contained inside DNA string '{}'?",
dna
);
}
6 changes: 3 additions & 3 deletions exercises/nucleotide-count/tests/nucleotide-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ fn test_count_empty() {
#[test]
#[ignore]
fn count_invalid_nucleotide() {
assert!(dna::count('X', "A").is_err());
assert_eq!(dna::count('X', "A"), Err('X'));
}

#[test]
#[ignore]
fn count_invalid_dna() {
assert!(dna::count('A', "AX").is_err());
assert_eq!(dna::count('A', "AX"), Err('X'));
}

#[test]
Expand Down Expand Up @@ -81,5 +81,5 @@ fn test_nucleotide_count_counts_all() {
#[test]
#[ignore]
fn counts_invalid_nucleotide_results_in_err() {
assert!(dna::nucleotide_counts("GGXXX").is_err());
assert_eq!(dna::nucleotide_counts("GGXXX"), Err('X'));
}