Solve proverb
This commit is contained in:
parent
c4e1e274f9
commit
35a2ab0b14
8 changed files with 281 additions and 0 deletions
36
rust/proverb/.exercism/config.json
Normal file
36
rust/proverb/.exercism/config.json
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"sacherjj"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"attilahorvath",
|
||||||
|
"coriolinus",
|
||||||
|
"cwhakes",
|
||||||
|
"efx",
|
||||||
|
"ErikSchierboom",
|
||||||
|
"leoyvens",
|
||||||
|
"lutostag",
|
||||||
|
"navossoc",
|
||||||
|
"nfiles",
|
||||||
|
"petertseng",
|
||||||
|
"rofrol",
|
||||||
|
"stringparser",
|
||||||
|
"xakon",
|
||||||
|
"ZapAnton"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/lib.rs",
|
||||||
|
"Cargo.toml"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"tests/proverb.rs"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.rs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
|
||||||
|
"source": "Wikipedia",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
|
||||||
|
}
|
1
rust/proverb/.exercism/metadata.json
Normal file
1
rust/proverb/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"rust","exercise":"proverb","id":"bd708801dcf04f46a0875d7bee07efe9","url":"https://exercism.org/tracks/rust/exercises/proverb","handle":"averywinters","is_requester":true,"auto_approve":false}
|
8
rust/proverb/.gitignore
vendored
Normal file
8
rust/proverb/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||||
|
Cargo.lock
|
4
rust/proverb/Cargo.toml
Normal file
4
rust/proverb/Cargo.toml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "proverb"
|
||||||
|
version = "1.1.0"
|
86
rust/proverb/HELP.md
Normal file
86
rust/proverb/HELP.md
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Execute the tests with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ cargo test
|
||||||
|
```
|
||||||
|
|
||||||
|
All but the first test have been ignored. After you get the first test to
|
||||||
|
pass, open the tests source file which is located in the `tests` directory
|
||||||
|
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||||
|
again. Each separate test is a function with `#[test]` flag above it.
|
||||||
|
Continue, until you pass every test.
|
||||||
|
|
||||||
|
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ cargo test -- --ignored
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ cargo test -- --include-ignored
|
||||||
|
```
|
||||||
|
|
||||||
|
To run a specific test, for example `some_test`, you can use:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ cargo test some_test
|
||||||
|
```
|
||||||
|
|
||||||
|
If the specific test is ignored, use:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ cargo test some_test -- --ignored
|
||||||
|
```
|
||||||
|
|
||||||
|
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||||
|
|
||||||
|
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||||
|
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
## Rust Installation
|
||||||
|
|
||||||
|
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||||
|
resources.
|
||||||
|
|
||||||
|
## Submitting the solution
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Feedback, Issues, Pull Requests
|
||||||
|
|
||||||
|
The GitHub [track repository][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!
|
||||||
|
|
||||||
|
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||||
|
|
||||||
|
[help-page]: https://exercism.org/tracks/rust/learning
|
||||||
|
[github]: https://github.com/exercism/rust
|
||||||
|
[contribution guide]: https://exercism.org/docs/community/contributors
|
51
rust/proverb/README.md
Normal file
51
rust/proverb/README.md
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
# Proverb
|
||||||
|
|
||||||
|
Welcome to Proverb on Exercism's Rust Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
|
||||||
|
|
||||||
|
Given 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:
|
||||||
|
|
||||||
|
```text
|
||||||
|
For want of a nail the shoe was lost.
|
||||||
|
For want of a shoe the horse was lost.
|
||||||
|
For want of a horse the rider was lost.
|
||||||
|
For want of a rider the message was lost.
|
||||||
|
For want of a message the battle was lost.
|
||||||
|
For want of a battle the kingdom was lost.
|
||||||
|
And all for the want of a nail.
|
||||||
|
```
|
||||||
|
|
||||||
|
Note 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.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @sacherjj
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @attilahorvath
|
||||||
|
- @coriolinus
|
||||||
|
- @cwhakes
|
||||||
|
- @efx
|
||||||
|
- @ErikSchierboom
|
||||||
|
- @leoyvens
|
||||||
|
- @lutostag
|
||||||
|
- @navossoc
|
||||||
|
- @nfiles
|
||||||
|
- @petertseng
|
||||||
|
- @rofrol
|
||||||
|
- @stringparser
|
||||||
|
- @xakon
|
||||||
|
- @ZapAnton
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Wikipedia - https://en.wikipedia.org/wiki/For_Want_of_a_Nail
|
20
rust/proverb/src/lib.rs
Normal file
20
rust/proverb/src/lib.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
pub fn build_proverb(list: &[&str]) -> String {
|
||||||
|
let body = list.windows(2).flat_map(|window| {
|
||||||
|
let [first, second] = window else {
|
||||||
|
unreachable!("window size was incorrect");
|
||||||
|
};
|
||||||
|
["For want of a ", first, " the ", second, " was lost.\n"]
|
||||||
|
});
|
||||||
|
let tail = list
|
||||||
|
.first()
|
||||||
|
.map(|first| ["And all for the want of a ", first, "."])
|
||||||
|
.into_iter()
|
||||||
|
.flatten();
|
||||||
|
let full = body.chain(tail);
|
||||||
|
// Plain collect won't pre-allocate the capacity, but we can
|
||||||
|
// to avoid a bunch of growth logic. Cloning the iterator is cheap.
|
||||||
|
let len = full.clone().map(str::len).sum();
|
||||||
|
let mut proverb = String::with_capacity(len);
|
||||||
|
proverb.extend(full);
|
||||||
|
proverb
|
||||||
|
}
|
75
rust/proverb/tests/proverb.rs
Normal file
75
rust/proverb/tests/proverb.rs
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
use proverb::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_pieces() {
|
||||||
|
let input = &[];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected = String::new();
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn one_piece() {
|
||||||
|
let input = &["nail"];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected: String = ["And all for the want of a nail."].join("\n");
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn two_pieces() {
|
||||||
|
let input = &["nail", "shoe"];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected: String = [
|
||||||
|
"For want of a nail the shoe was lost.",
|
||||||
|
"And all for the want of a nail.",
|
||||||
|
]
|
||||||
|
.join("\n");
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn three_pieces() {
|
||||||
|
let input = &["nail", "shoe", "horse"];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected: String = [
|
||||||
|
"For want of a nail the shoe was lost.",
|
||||||
|
"For want of a shoe the horse was lost.",
|
||||||
|
"And all for the want of a nail.",
|
||||||
|
]
|
||||||
|
.join("\n");
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn full_proverb() {
|
||||||
|
let input = &[
|
||||||
|
"nail", "shoe", "horse", "rider", "message", "battle", "kingdom",
|
||||||
|
];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected: String = [
|
||||||
|
"For want of a nail the shoe was lost.",
|
||||||
|
"For want of a shoe the horse was lost.",
|
||||||
|
"For want of a horse the rider was lost.",
|
||||||
|
"For want of a rider the message was lost.",
|
||||||
|
"For want of a message the battle was lost.",
|
||||||
|
"For want of a battle the kingdom was lost.",
|
||||||
|
"And all for the want of a nail.",
|
||||||
|
]
|
||||||
|
.join("\n");
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn four_pieces_modernized() {
|
||||||
|
let input = &["pin", "gun", "soldier", "battle"];
|
||||||
|
let output = build_proverb(input);
|
||||||
|
let expected: String = [
|
||||||
|
"For want of a pin the gun was lost.",
|
||||||
|
"For want of a gun the soldier was lost.",
|
||||||
|
"For want of a soldier the battle was lost.",
|
||||||
|
"And all for the want of a pin.",
|
||||||
|
]
|
||||||
|
.join("\n");
|
||||||
|
assert_eq!(output, expected);
|
||||||
|
}
|
Loading…
Reference in a new issue