Skip to content

Commit

Permalink
Nucleotide count
Browse files Browse the repository at this point in the history
  • Loading branch information
vhgcuong committed Oct 18, 2023
1 parent 4f8bb44 commit 88683b3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/nucleotide_count.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
use std::collections::HashMap;

const DNA: [char; 4] = ['G', 'C', 'T', 'A'];

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

let mut count = 0;
for ch in dna.chars() {
if !DNA.contains(&ch) {
return Err(ch);
}

if ch == nucleotide {
count += 1;
}
}

Ok(count)
}

pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
todo!("How much of every nucleotide type is contained inside DNA string '{dna}'?");
let mut result: HashMap<char, usize> = HashMap::from([('A', 0), ('T', 0), ('C', 0), ('G', 0)]);

for ch in dna.chars() {
if !DNA.contains(&ch) {
return Err(ch);
}

*result.entry(ch).or_default() += 1;
}

Ok(result)
}

0 comments on commit 88683b3

Please sign in to comment.