Skip to content

Instantly share code, notes, and snippets.

@NickyMeuleman
Created July 8, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickyMeuleman/d180c99264863c2917b4006be5a78370 to your computer and use it in GitHub Desktop.
Save NickyMeuleman/d180c99264863c2917b4006be5a78370 to your computer and use it in GitHub Desktop.
exercism.io Rust nucleotide-count
use std::collections::HashMap;
const VALID: [char; 4] = ['A', 'C', 'G', 'T'];
pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
if !VALID.contains(&nucleotide) {
return Err(nucleotide);
}
match dna.chars().find(|c| !VALID.contains(c)) {
Some(c) => Err(c),
None => Ok(dna.matches(nucleotide).count()),
}
}
pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
VALID
.iter()
.map(|&c| {
let num = count(c, dna)?;
Ok((c, num))
})
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment