-
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
1 changed file
with
29 additions
and
2 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 |
---|---|---|
@@ -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) | ||
} |