Skip to content

Commit ede62b2

Browse files
committed
refactor
1 parent 02f3886 commit ede62b2

File tree

1 file changed

+7
-18
lines changed
  • rust/nucleotide-count/src

1 file changed

+7
-18
lines changed

rust/nucleotide-count/src/lib.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
22

33
pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
44
if !valid_nucleotide(nucleotide) {
5-
return Err(nucleotide)
5+
return Err(nucleotide);
66
}
77
let counts = nucleotide_counts(dna)?;
88
Ok(*counts.get(&nucleotide).unwrap())
@@ -11,7 +11,7 @@ pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
1111
pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
1212
for c in dna.chars() {
1313
if !valid_nucleotide(c) {
14-
return Err(c)
14+
return Err(c);
1515
}
1616
}
1717
let mut counts: HashMap<char, usize> = default_map();
@@ -24,21 +24,10 @@ pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
2424
}
2525

2626
fn default_map() -> HashMap<char, usize> {
27-
let result = HashMap::from([
28-
('A', 0),
29-
('C', 0),
30-
('G', 0),
31-
('T', 0),
32-
]);
33-
result
27+
HashMap::from([('A', 0), ('C', 0), ('G', 0), ('T', 0)])
3428
}
3529

36-
fn valid_nucleotide(nucleotide: char) -> bool {
37-
return match nucleotide {
38-
'A' => true,
39-
'C' => true,
40-
'G' => true,
41-
'T' => true,
42-
_ => false,
43-
}
30+
fn valid_nucleotide(c: char) -> bool {
31+
let nucleotides = HashSet::from(['A', 'C', 'G', 'T']);
32+
nucleotides.contains(&c)
4433
}

0 commit comments

Comments
 (0)