aboutsummaryrefslogtreecommitdiff
path: root/nucleotide-count
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 17:22:21 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit4e2052c4d792540c2f742b2c2a081b11117ed41d (patch)
tree6fdfbfd3b6f1c06cf613de2f6f052d39d93ec5e7 /nucleotide-count
parent8a61a513dad5c22bb5596b8918a2d03755d08d1e (diff)
downloadexercism-4e2052c4d792540c2f742b2c2a081b11117ed41d.tar.gz
exercism-4e2052c4d792540c2f742b2c2a081b11117ed41d.zip
[rust] Nucleotide Count
Diffstat (limited to 'nucleotide-count')
-rw-r--r--nucleotide-count/.exercism/metadata.json1
-rw-r--r--nucleotide-count/.gitignore8
-rw-r--r--nucleotide-count/Cargo.toml7
-rw-r--r--nucleotide-count/README.md110
-rw-r--r--nucleotide-count/src/lib.rs21
-rw-r--r--nucleotide-count/tests/nucleotide-count.rs88
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
8Cargo.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]
2edition = "2018"
3name = "nucleotide-count"
4version = "1.3.0"
5
6[dependencies]
7maplit = "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
3Each of us inherits from our biological parents a set of chemical
4instructions known as DNA that influence how our bodies are constructed.
5All 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
10DNA is a long chain of other chemicals and the most important are the
11four nucleotides, adenine, cytosine, guanine and thymine. A single DNA
12chain can contain billions of these four nucleotides and the order in
13which they occur is important! We call the order of these nucleotides in
14a bit of DNA a "DNA sequence".
15
16We represent a DNA sequence as an ordered collection of these four
17nucleotides and a common way to do that is with a string of characters
18such as "ATTACG" for a DNA sequence of 6 nucleotides. 'A' for adenine,
19'C' for cytosine, 'G' for guanine, and 'T' for thymine.
20
21Given a string representing a DNA sequence, count how many of each
22nucleotide is present. If the string contains characters that aren't A,
23C, G, or T then it is invalid and you should signal an error.
24
25For example:
26
27```
28"GATTACA" -> 'A': 3, 'C': 1, 'G': 1, 'T': 2
29"INVALID" -> error
30```
31
32## Rust Installation
33
34Refer to the [exercism help page][help-page] for Rust installation and learning
35resources.
36
37## Writing the Code
38
39Execute the tests with:
40
41```bash
42$ cargo test
43```
44
45All but the first test have been ignored. After you get the first test to
46pass, open the tests source file which is located in the `tests` directory
47and remove the `#[ignore]` flag from the next test and get the tests to pass
48again. Each separate test is a function with `#[test]` flag above it.
49Continue, until you pass every test.
50
51If you wish to run all ignored tests without editing the tests source file, use:
52
53```bash
54$ cargo test -- --ignored
55```
56
57To run a specific test, for example `some_test`, you can use:
58
59```bash
60$ cargo test some_test
61```
62
63If the specific test is ignored use:
64
65```bash
66$ cargo test some_test -- --ignored
67```
68
69To learn more about Rust tests refer to the [online test documentation][rust-tests]
70
71Make sure to read the [Modules][modules] chapter if you
72haven't already, it will help you with organizing your files.
73
74## Further improvements
75
76After 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
78To format your solution, inside the solution directory use
79
80```bash
81cargo fmt
82```
83
84To see, if your solution contains some common ineffective use cases, inside the solution directory use
85
86```bash
87cargo clippy --all-targets
88```
89
90## Submitting the solution
91
92Generally 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
96The [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
98If 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
107The Calculating DNA Nucleotides_problem at Rosalind [http://rosalind.info/problems/dna/](http://rosalind.info/problems/dna/)
108
109## Submitting Incomplete Solutions
110It'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 @@
1use maplit::hashmap;
2use std::collections::HashMap;
3
4pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
5 nucleotide_counts(dna)?
6 .remove(&nucleotide)
7 .ok_or(nucleotide)
8}
9
10pub 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 @@
1use nucleotide_count as dna;
2
3use std::collections::HashMap;
4
5fn 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]
20fn count_returns_result() {
21 assert!(dna::count('A', "").is_ok());
22}
23
24#[test]
25fn test_count_empty() {
26 assert_eq!(dna::count('A', ""), Ok(0));
27}
28
29#[test]
30fn count_invalid_nucleotide() {
31 assert_eq!(dna::count('X', "A"), Err('X'));
32}
33
34#[test]
35fn count_invalid_dna() {
36 assert_eq!(dna::count('A', "AX"), Err('X'));
37}
38
39#[test]
40fn test_count_repetitive_cytosine() {
41 assert_eq!(dna::count('C', "CCCCC"), Ok(5));
42}
43
44#[test]
45fn test_count_only_thymine() {
46 assert_eq!(dna::count('T', "GGGGGTAACCCGG"), Ok(1));
47}
48
49#[test]
50fn counts_returns_result() {
51 assert!(dna::nucleotide_counts("ACGT").is_ok());
52}
53
54#[test]
55fn 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
61fn 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]
66fn 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
72fn 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]
80fn counts_invalid_nucleotide_results_in_err() {
81 assert_eq!(dna::nucleotide_counts("GGXXX"), Err('X'));
82}
83
84#[test]
85/// strand with invalid nucleotides
86fn test_strand_with_invalid_nucleotides() {
87 assert_eq!(dna::nucleotide_counts("AGXXACT"), Err('X'),);
88}