aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--proverb/.exercism/metadata.json1
-rw-r--r--proverb/.gitignore8
-rw-r--r--proverb/Cargo.toml6
-rw-r--r--proverb/README.md97
-rw-r--r--proverb/src/lib.rs15
-rw-r--r--proverb/tests/proverb.rs70
6 files changed, 197 insertions, 0 deletions
diff --git a/proverb/.exercism/metadata.json b/proverb/.exercism/metadata.json
new file mode 100644
index 0000000..92c8e56
--- /dev/null
+++ b/proverb/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"proverb","id":"df96008ca5da43758fca9ac1ee80607e","url":"https://exercism.io/my/solutions/df96008ca5da43758fca9ac1ee80607e","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/proverb/.gitignore b/proverb/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/proverb/.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/proverb/Cargo.toml b/proverb/Cargo.toml
new file mode 100644
index 0000000..4e09ea1
--- /dev/null
+++ b/proverb/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2edition = "2018"
3name = "proverb"
4version = "1.1.0"
5
6[dependencies]
diff --git a/proverb/README.md b/proverb/README.md
new file mode 100644
index 0000000..b9efe0b
--- /dev/null
+++ b/proverb/README.md
@@ -0,0 +1,97 @@
1# Proverb
2
3For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
4
5Given a list of inputs, generate the relevant proverb. For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:
6
7```text
8For want of a nail the shoe was lost.
9For want of a shoe the horse was lost.
10For want of a horse the rider was lost.
11For want of a rider the message was lost.
12For want of a message the battle was lost.
13For want of a battle the kingdom was lost.
14And all for the want of a nail.
15```
16
17Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.
18
19## Rust Installation
20
21Refer to the [exercism help page][help-page] for Rust installation and learning
22resources.
23
24## Writing the Code
25
26Execute the tests with:
27
28```bash
29$ cargo test
30```
31
32All but the first test have been ignored. After you get the first test to
33pass, open the tests source file which is located in the `tests` directory
34and remove the `#[ignore]` flag from the next test and get the tests to pass
35again. Each separate test is a function with `#[test]` flag above it.
36Continue, until you pass every test.
37
38If you wish to run all ignored tests without editing the tests source file, use:
39
40```bash
41$ cargo test -- --ignored
42```
43
44To run a specific test, for example `some_test`, you can use:
45
46```bash
47$ cargo test some_test
48```
49
50If the specific test is ignored use:
51
52```bash
53$ cargo test some_test -- --ignored
54```
55
56To learn more about Rust tests refer to the [online test documentation][rust-tests]
57
58Make sure to read the [Modules][modules] chapter if you
59haven't already, it will help you with organizing your files.
60
61## Further improvements
62
63After 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.
64
65To format your solution, inside the solution directory use
66
67```bash
68cargo fmt
69```
70
71To see, if your solution contains some common ineffective use cases, inside the solution directory use
72
73```bash
74cargo clippy --all-targets
75```
76
77## Submitting the solution
78
79Generally 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.
80
81## Feedback, Issues, Pull Requests
82
83The [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!
84
85If 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).
86
87[help-page]: https://exercism.io/tracks/rust/learning
88[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
89[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
90[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
91
92## Source
93
94Wikipedia [http://en.wikipedia.org/wiki/For_Want_of_a_Nail](http://en.wikipedia.org/wiki/For_Want_of_a_Nail)
95
96## Submitting Incomplete Solutions
97It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/proverb/src/lib.rs b/proverb/src/lib.rs
new file mode 100644
index 0000000..1046553
--- /dev/null
+++ b/proverb/src/lib.rs
@@ -0,0 +1,15 @@
1use std::iter;
2
3pub fn build_proverb(list: &[&str]) -> String {
4 if list.is_empty() {
5 String::new()
6 } else {
7 let reason = |(a, b)| format!("For want of a {} the {} was lost.\n", a, b);
8 let bitter_end = format!("And all for the want of a {}.", list[0]);
9 list.iter()
10 .zip(list.iter().skip(1)) // .window(2) also works here
11 .map(reason)
12 .chain(iter::once(bitter_end))
13 .collect()
14 }
15}
diff --git a/proverb/tests/proverb.rs b/proverb/tests/proverb.rs
new file mode 100644
index 0000000..220de45
--- /dev/null
+++ b/proverb/tests/proverb.rs
@@ -0,0 +1,70 @@
1use proverb::build_proverb;
2
3#[test]
4fn test_two_pieces() {
5 let input = vec!["nail", "shoe"];
6 let expected = vec![
7 "For want of a nail the shoe was lost.",
8 "And all for the want of a nail.",
9 ]
10 .join("\n");
11 assert_eq!(build_proverb(&input), expected);
12}
13
14// Notice the change in the last line at three pieces.
15#[test]
16fn test_three_pieces() {
17 let input = vec!["nail", "shoe", "horse"];
18 let expected = vec![
19 "For want of a nail the shoe was lost.",
20 "For want of a shoe the horse was lost.",
21 "And all for the want of a nail.",
22 ]
23 .join("\n");
24 assert_eq!(build_proverb(&input), expected);
25}
26
27#[test]
28fn test_one_piece() {
29 let input = vec!["nail"];
30 let expected = String::from("And all for the want of a nail.");
31 assert_eq!(build_proverb(&input), expected);
32}
33
34#[test]
35fn test_zero_pieces() {
36 let input: Vec<&str> = vec![];
37 let expected = String::new();
38 assert_eq!(build_proverb(&input), expected);
39}
40
41#[test]
42fn test_full() {
43 let input = vec![
44 "nail", "shoe", "horse", "rider", "message", "battle", "kingdom",
45 ];
46 let expected = vec![
47 "For want of a nail the shoe was lost.",
48 "For want of a shoe the horse was lost.",
49 "For want of a horse the rider was lost.",
50 "For want of a rider the message was lost.",
51 "For want of a message the battle was lost.",
52 "For want of a battle the kingdom was lost.",
53 "And all for the want of a nail.",
54 ]
55 .join("\n");
56 assert_eq!(build_proverb(&input), expected);
57}
58
59#[test]
60fn test_three_pieces_modernized() {
61 let input = vec!["pin", "gun", "soldier", "battle"];
62 let expected = vec![
63 "For want of a pin the gun was lost.",
64 "For want of a gun the soldier was lost.",
65 "For want of a soldier the battle was lost.",
66 "And all for the want of a pin.",
67 ]
68 .join("\n");
69 assert_eq!(build_proverb(&input), expected);
70}