diff options
Diffstat (limited to 'nucleotide-count')
| -rw-r--r-- | nucleotide-count/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | nucleotide-count/.gitignore | 8 | ||||
| -rw-r--r-- | nucleotide-count/Cargo.toml | 7 | ||||
| -rw-r--r-- | nucleotide-count/README.md | 110 | ||||
| -rw-r--r-- | nucleotide-count/src/lib.rs | 21 | ||||
| -rw-r--r-- | nucleotide-count/tests/nucleotide-count.rs | 88 |
6 files changed, 235 insertions, 0 deletions
diff --git a/nucleotide-count/.exercism/metadata.json b/nucleotide-count/.exercism/metadata.json new file mode 100644 index 0000000..3cb94c8 --- /dev/null +++ b/nucleotide-count/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"nucleotide-count","id":"2e44b94b9e1648c2876e1882f15bd9de","url":"https://exercism.io/my/solutions/2e44b94b9e1648c2876e1882f15bd9de","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/nucleotide-count/.gitignore b/nucleotide-count/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/nucleotide-count/.gitignore | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/nucleotide-count/Cargo.toml b/nucleotide-count/Cargo.toml new file mode 100644 index 0000000..3eb8759 --- /dev/null +++ b/nucleotide-count/Cargo.toml | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "nucleotide-count" | ||
| 4 | version = "1.3.0" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | maplit = "1.0.1" | ||
diff --git a/nucleotide-count/README.md b/nucleotide-count/README.md new file mode 100644 index 0000000..9a85942 --- /dev/null +++ b/nucleotide-count/README.md | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | # Nucleotide Count | ||
| 2 | |||
| 3 | Each of us inherits from our biological parents a set of chemical | ||
| 4 | instructions known as DNA that influence how our bodies are constructed. | ||
| 5 | All known life depends on DNA! | ||
| 6 | |||
| 7 | > Note: You do not need to understand anything about nucleotides or DNA | ||
| 8 | > to complete this exercise. | ||
| 9 | |||
| 10 | DNA is a long chain of other chemicals and the most important are the | ||
| 11 | four nucleotides, adenine, cytosine, guanine and thymine. A single DNA | ||
| 12 | chain can contain billions of these four nucleotides and the order in | ||
| 13 | which they occur is important! We call the order of these nucleotides in | ||
| 14 | a bit of DNA a "DNA sequence". | ||
| 15 | |||
| 16 | We represent a DNA sequence as an ordered collection of these four | ||
| 17 | nucleotides and a common way to do that is with a string of characters | ||
| 18 | such as "ATTACG" for a DNA sequence of 6 nucleotides. 'A' for adenine, | ||
| 19 | 'C' for cytosine, 'G' for guanine, and 'T' for thymine. | ||
| 20 | |||
| 21 | Given a string representing a DNA sequence, count how many of each | ||
| 22 | nucleotide is present. If the string contains characters that aren't A, | ||
| 23 | C, G, or T then it is invalid and you should signal an error. | ||
| 24 | |||
| 25 | For example: | ||
| 26 | |||
| 27 | ``` | ||
| 28 | "GATTACA" -> 'A': 3, 'C': 1, 'G': 1, 'T': 2 | ||
| 29 | "INVALID" -> error | ||
| 30 | ``` | ||
| 31 | |||
| 32 | ## Rust Installation | ||
| 33 | |||
| 34 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 35 | resources. | ||
| 36 | |||
| 37 | ## Writing the Code | ||
| 38 | |||
| 39 | Execute the tests with: | ||
| 40 | |||
| 41 | ```bash | ||
| 42 | $ cargo test | ||
| 43 | ``` | ||
| 44 | |||
| 45 | All but the first test have been ignored. After you get the first test to | ||
| 46 | pass, open the tests source file which is located in the `tests` directory | ||
| 47 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 48 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 49 | Continue, until you pass every test. | ||
| 50 | |||
| 51 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 52 | |||
| 53 | ```bash | ||
| 54 | $ cargo test -- --ignored | ||
| 55 | ``` | ||
| 56 | |||
| 57 | To run a specific test, for example `some_test`, you can use: | ||
| 58 | |||
| 59 | ```bash | ||
| 60 | $ cargo test some_test | ||
| 61 | ``` | ||
| 62 | |||
| 63 | If the specific test is ignored use: | ||
| 64 | |||
| 65 | ```bash | ||
| 66 | $ cargo test some_test -- --ignored | ||
| 67 | ``` | ||
| 68 | |||
| 69 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 70 | |||
| 71 | Make sure to read the [Modules][modules] chapter if you | ||
| 72 | haven't already, it will help you with organizing your files. | ||
| 73 | |||
| 74 | ## Further improvements | ||
| 75 | |||
| 76 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 77 | |||
| 78 | To format your solution, inside the solution directory use | ||
| 79 | |||
| 80 | ```bash | ||
| 81 | cargo fmt | ||
| 82 | ``` | ||
| 83 | |||
| 84 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 85 | |||
| 86 | ```bash | ||
| 87 | cargo clippy --all-targets | ||
| 88 | ``` | ||
| 89 | |||
| 90 | ## Submitting the solution | ||
| 91 | |||
| 92 | Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. | ||
| 93 | |||
| 94 | ## Feedback, Issues, Pull Requests | ||
| 95 | |||
| 96 | The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! | ||
| 97 | |||
| 98 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 99 | |||
| 100 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 101 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 102 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 103 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 104 | |||
| 105 | ## Source | ||
| 106 | |||
| 107 | The Calculating DNA Nucleotides_problem at Rosalind [http://rosalind.info/problems/dna/](http://rosalind.info/problems/dna/) | ||
| 108 | |||
| 109 | ## Submitting Incomplete Solutions | ||
| 110 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/nucleotide-count/src/lib.rs b/nucleotide-count/src/lib.rs new file mode 100644 index 0000000..98b0c38 --- /dev/null +++ b/nucleotide-count/src/lib.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | use maplit::hashmap; | ||
| 2 | use std::collections::HashMap; | ||
| 3 | |||
| 4 | pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> { | ||
| 5 | nucleotide_counts(dna)? | ||
| 6 | .remove(&nucleotide) | ||
| 7 | .ok_or(nucleotide) | ||
| 8 | } | ||
| 9 | |||
| 10 | pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> { | ||
| 11 | dna.chars().fold( | ||
| 12 | Ok(hashmap![ 'A' => 0, 'C' => 0, 'G' => 0, 'T' => 0 ]), | ||
| 13 | |acc, n| match n { | ||
| 14 | 'A' | 'C' | 'G' | 'T' => acc.map(|mut hmap| { | ||
| 15 | hmap.entry(n).and_modify(|e| *e += 1).or_insert(0); | ||
| 16 | hmap | ||
| 17 | }), | ||
| 18 | _ => Err(n), | ||
| 19 | }, | ||
| 20 | ) | ||
| 21 | } | ||
diff --git a/nucleotide-count/tests/nucleotide-count.rs b/nucleotide-count/tests/nucleotide-count.rs new file mode 100644 index 0000000..8190580 --- /dev/null +++ b/nucleotide-count/tests/nucleotide-count.rs | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | use nucleotide_count as dna; | ||
| 2 | |||
| 3 | use std::collections::HashMap; | ||
| 4 | |||
| 5 | fn process_nucleotidecounts_case(s: &str, pairs: &[(char, usize)]) { | ||
| 6 | // The reason for the awkward code in here is to ensure that the failure | ||
| 7 | // message for assert_eq! is as informative as possible. A simpler | ||
| 8 | // solution would simply check the length of the map, and then | ||
| 9 | // check for the presence and value of each key in the given pairs vector. | ||
| 10 | let mut m: HashMap<char, usize> = dna::nucleotide_counts(s).unwrap(); | ||
| 11 | for &(k, v) in pairs.iter() { | ||
| 12 | assert_eq!((k, m.remove(&k)), (k, Some(v))); | ||
| 13 | } | ||
| 14 | |||
| 15 | // may fail with a message that clearly shows all extra pairs in the map | ||
| 16 | assert_eq!(m.iter().collect::<Vec<(&char, &usize)>>(), vec![]); | ||
| 17 | } | ||
| 18 | |||
| 19 | #[test] | ||
| 20 | fn count_returns_result() { | ||
| 21 | assert!(dna::count('A', "").is_ok()); | ||
| 22 | } | ||
| 23 | |||
| 24 | #[test] | ||
| 25 | fn test_count_empty() { | ||
| 26 | assert_eq!(dna::count('A', ""), Ok(0)); | ||
| 27 | } | ||
| 28 | |||
| 29 | #[test] | ||
| 30 | fn count_invalid_nucleotide() { | ||
| 31 | assert_eq!(dna::count('X', "A"), Err('X')); | ||
| 32 | } | ||
| 33 | |||
| 34 | #[test] | ||
| 35 | fn count_invalid_dna() { | ||
| 36 | assert_eq!(dna::count('A', "AX"), Err('X')); | ||
| 37 | } | ||
| 38 | |||
| 39 | #[test] | ||
| 40 | fn test_count_repetitive_cytosine() { | ||
| 41 | assert_eq!(dna::count('C', "CCCCC"), Ok(5)); | ||
| 42 | } | ||
| 43 | |||
| 44 | #[test] | ||
| 45 | fn test_count_only_thymine() { | ||
| 46 | assert_eq!(dna::count('T', "GGGGGTAACCCGG"), Ok(1)); | ||
| 47 | } | ||
| 48 | |||
| 49 | #[test] | ||
| 50 | fn counts_returns_result() { | ||
| 51 | assert!(dna::nucleotide_counts("ACGT").is_ok()); | ||
| 52 | } | ||
| 53 | |||
| 54 | #[test] | ||
| 55 | fn test_empty_strand() { | ||
| 56 | process_nucleotidecounts_case("", &[('A', 0), ('T', 0), ('C', 0), ('G', 0)]); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | /// can count one nucleotide in single-character input | ||
| 61 | fn test_can_count_one_nucleotide_in_singlecharacter_input() { | ||
| 62 | process_nucleotidecounts_case("G", &[('A', 0), ('C', 0), ('G', 1), ('T', 0)]); | ||
| 63 | } | ||
| 64 | |||
| 65 | #[test] | ||
| 66 | fn test_strand_with_repeated_nucleotide() { | ||
| 67 | process_nucleotidecounts_case("GGGGGGG", &[('A', 0), ('T', 0), ('C', 0), ('G', 7)]); | ||
| 68 | } | ||
| 69 | |||
| 70 | #[test] | ||
| 71 | /// strand with multiple nucleotides | ||
| 72 | fn test_strand_with_multiple_nucleotides() { | ||
| 73 | process_nucleotidecounts_case( | ||
| 74 | "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC", | ||
| 75 | &[('A', 20), ('T', 21), ('C', 12), ('G', 17)], | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | #[test] | ||
| 80 | fn counts_invalid_nucleotide_results_in_err() { | ||
| 81 | assert_eq!(dna::nucleotide_counts("GGXXX"), Err('X')); | ||
| 82 | } | ||
| 83 | |||
| 84 | #[test] | ||
| 85 | /// strand with invalid nucleotides | ||
| 86 | fn test_strand_with_invalid_nucleotides() { | ||
| 87 | assert_eq!(dna::nucleotide_counts("AGXXACT"), Err('X'),); | ||
| 88 | } | ||
