Created
July 8, 2021 15:34
-
-
Save NickyMeuleman/d180c99264863c2917b4006be5a78370 to your computer and use it in GitHub Desktop.
exercism.io Rust nucleotide-count
This file contains hidden or 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
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