-
Notifications
You must be signed in to change notification settings - Fork 543
nucleotide-count: Error on invalid nucleotide #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| pub fn count(nucleotide: char, input: &str) -> usize { | ||
| input.chars().filter(|&c| c == nucleotide).count() | ||
| static VALID_NUCLEOTIDES: &'static str = "ACGT"; | ||
|
|
||
| pub fn count(nucleotide: char, input: &str) -> Result<usize, char> { | ||
| let valid = |x: char| { VALID_NUCLEOTIDES.contains(x) }; | ||
| if valid(nucleotide) && input.chars().all(valid) { | ||
| Ok(input.chars().filter(|&c| c == nucleotide).count()) | ||
| } else { | ||
| Err(nucleotide) | ||
| } | ||
| } | ||
|
|
||
| pub fn nucleotide_counts(input: &str) -> HashMap<char, usize> { | ||
| let mut map: HashMap<char, usize> = "ACGT".chars().map(|c| (c, 0)).collect(); | ||
| pub fn nucleotide_counts(input: &str) -> Result<HashMap<char, usize>, char> { | ||
| let mut map: HashMap<char, usize> = VALID_NUCLEOTIDES.chars().map(|c| (c, 0)).collect(); | ||
| for nucleotide in input.chars() { | ||
| *map.entry(nucleotide).or_insert(0) += 1; | ||
| if let Some(n) = map.get_mut(&nucleotide) { | ||
| *n += 1; | ||
| } else { | ||
| return Err(nucleotide); | ||
| } | ||
| } | ||
| map | ||
| Ok(map) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ fn check_dna(s: &str, pairs: &[(char, usize)]) { | |
| // message for assert_eq! is as informative as possible. A simpler | ||
| // solution would simply check the length of the map, and then | ||
| // check for the presence and value of each key in the given pairs vector. | ||
| let mut m: HashMap<char, usize> = dna::nucleotide_counts(s); | ||
| let mut m: HashMap<char, usize> = dna::nucleotide_counts(s).unwrap(); | ||
| for &(k, v) in pairs.iter() { | ||
| assert_eq!((k, m.remove(&k).unwrap()), (k, v)); | ||
| } | ||
|
|
@@ -16,20 +16,44 @@ fn check_dna(s: &str, pairs: &[(char, usize)]) { | |
| } | ||
|
|
||
| #[test] | ||
| fn count_returns_result() { | ||
| assert!(dna::count('A', "").is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn test_count_empty() { | ||
| assert_eq!(dna::count('A', ""), 0); | ||
| assert_eq!(dna::count('A', "").unwrap(), 0); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we do anything specific here to make abundandly clear that it's a result? see https://github.com/exercism/xrust/blob/master/exercises/largest-series-product/tests/largest-series-product.rs#L6
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. For implementations that expect Result or Option, I usually start with some
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. One for each function. |
||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn count_invalid_nucleotide() { | ||
| assert!(dna::count('X', "A").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn count_invalid_dna() { | ||
| assert!(dna::count('A', "AX").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn test_count_repetitive_cytosine() { | ||
| assert_eq!(dna::count('C', "CCCCC"), 5); | ||
| assert_eq!(dna::count('C', "CCCCC").unwrap(), 5); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn test_count_only_thymine() { | ||
| assert_eq!(dna::count('T', "GGGGGTAACCCGG"), 1); | ||
| assert_eq!(dna::count('T', "GGGGGTAACCCGG").unwrap(), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn counts_returns_result() { | ||
| assert!(dna::nucleotide_counts("ACGT").is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -56,3 +80,9 @@ fn test_nucleotide_count_counts_all() { | |
| GAGTGTCTGATAGCAGC", | ||
| &[('A', 20), ('T', 21), ('C', 12), ('G', 17)]); | ||
| } | ||
|
|
||
| #[test] | ||
| #[ignore] | ||
| fn counts_invalid_nucleotide_results_in_err() { | ||
| assert!(dna::nucleotide_counts("GGXXX").is_err()); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ hamming | Result | |
| pascals-triangle | Math, Vec, Index (optional) | ||
| scrabble-score | chaining higher-order functions, HashMap (optional) | ||
| pangram | filter, ascii (optional) | ||
| nucleotide-count | filter, entry api, mutablity, match | ||
| nucleotide-count | Result, filter, entry api, mutablity, match | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duped, but to be solved by #236 |
||
| largest-series-product | Result, windows, higher-order functions, char | ||
| word-count | hashmap, str vs string, chars, entry api | ||
| atbash-cipher | str vs string, primitive types, iterators, chars, ascii | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for
count('A', "AX")this returnsAinstead ofX, but we don't check it in the tests so I'm not motivated.