Rust - Proverb Complete
This commit is contained in:
parent
8f48809895
commit
9e541956ec
5 changed files with 105 additions and 37 deletions
|
@ -28,7 +28,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(emptyList()))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testNumbersAreJustPushedOntoTheStack() {
|
||||
assertEquals(
|
||||
|
@ -36,7 +35,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 3 4 5")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testTwoNumbersCanBeAdded() {
|
||||
assertEquals(
|
||||
|
@ -44,7 +42,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 +")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfAdditionAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -53,7 +50,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("+"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfAdditionAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -62,7 +58,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 +"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testTwoNumbersCanBeSubtracted() {
|
||||
assertEquals(
|
||||
|
@ -70,7 +65,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("3 4 -")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfSubtractionAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -79,7 +73,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("-"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfSubtractionAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -88,7 +81,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 -"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testTwoNumbersCanBeMultiplied() {
|
||||
assertEquals(
|
||||
|
@ -96,7 +88,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("2 4 *")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfMultiplicationAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -105,7 +96,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("*"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfMultiplicationAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -114,7 +104,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 *"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testTwoNumbersCanBeDivided() {
|
||||
assertEquals(
|
||||
|
@ -122,7 +111,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("12 3 /")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testThatIntegerDivisionIsUsed() {
|
||||
assertEquals(
|
||||
|
@ -130,7 +118,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("8 3 /")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfDividingByZero() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -139,7 +126,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("4 0 /"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfDivisionAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -148,7 +134,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("/"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfDivisionAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -157,7 +142,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 /"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCombinedAdditionAndSubtraction() {
|
||||
assertEquals(
|
||||
|
@ -165,7 +149,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 + 4 -")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCombinedMultiplicationAndDivision() {
|
||||
assertEquals(
|
||||
|
@ -173,7 +156,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("2 4 * 3 /")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testDupCopiesTheTopValueOnTheStack() {
|
||||
assertEquals(
|
||||
|
@ -181,7 +163,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 DUP")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testDupParsingIsCaseInsensitive() {
|
||||
assertEquals(
|
||||
|
@ -189,7 +170,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 Dup")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfDuplicatingAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -198,7 +178,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("dup"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testDropRemovesTheTopValueOnTheStackIfItIsTheOnlyOne() {
|
||||
assertEquals(
|
||||
|
@ -206,7 +185,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 drop")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testDropRemovesTheTopValueOnTheStackIfItIsNotTheOnlyOne() {
|
||||
assertEquals(
|
||||
|
@ -214,7 +192,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 drop")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfDroppingAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -223,7 +200,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("drop"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreTheOnlyOnes() {
|
||||
assertEquals(
|
||||
|
@ -231,7 +207,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 swap")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreNotTheOnlyOnes() {
|
||||
assertEquals(
|
||||
|
@ -239,7 +214,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 3 swap")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfSwappingAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -248,7 +222,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("swap"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfSwappingAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -257,7 +230,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 swap"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testOverCopiesTheSecondElementIfThereAreOnlyTwo() {
|
||||
assertEquals(
|
||||
|
@ -265,7 +237,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 over")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testOverCopiesTheSecondElementIfThereAreMoreThanTwo() {
|
||||
assertEquals(
|
||||
|
@ -273,7 +244,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 2 3 over")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfOveringAttemptedWithNothingOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -282,7 +252,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("over"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testErrorIfOveringAttemptedWithOneNumberOnTheStack() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
@ -291,7 +260,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf("1 over"))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testUserDefinedOperatorsCanConsistOfBuiltInOperators() {
|
||||
assertEquals(
|
||||
|
@ -299,7 +267,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf(": dup-twice dup dup ;", "1 dup-twice")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testUserDefinedOperatorsAreEvaluatedInTheCorrectOrder() {
|
||||
assertEquals(
|
||||
|
@ -307,7 +274,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf(": countup 1 2 3 ;", "countup")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCanRedefineAUserDefinedOperator() {
|
||||
assertEquals(
|
||||
|
@ -315,7 +281,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf(": foo dup ;", ": foo dup dup ;", "1 foo")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCanOverrideBuiltInWordOperators() {
|
||||
assertEquals(
|
||||
|
@ -323,7 +288,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf(": swap dup ;", "1 swap")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCanOverrideBuiltInArithmeticOperators() {
|
||||
assertEquals(
|
||||
|
@ -331,7 +295,6 @@ class ForthEvaluatorTest {
|
|||
forthEvaluator.evaluateProgram(listOf(": + * ;", "3 4 +")))
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
fun testCannotRedefineNumbers() {
|
||||
expectedException.expect(IllegalArgumentException::class.java)
|
||||
|
|
6
rust/proverb/Cargo.toml
Normal file
6
rust/proverb/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "proverb"
|
||||
version = "0.0.0"
|
||||
authors = ["sacherjj <sacherjj@gmail.com>"]
|
||||
|
||||
[dependencies]
|
50
rust/proverb/README.md
Normal file
50
rust/proverb/README.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Proverb
|
||||
|
||||
For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output
|
||||
the full text of this proverbial rhyme:
|
||||
|
||||
> 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 horseshoe nail.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Writing the Code
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests
|
||||
to pass again. The test file is located in the `tests` directory. You can
|
||||
also remove the ignore flag from all the tests to get them to run all at once
|
||||
if you wish.
|
||||
|
||||
Make sure to read the [Crates and Modules](https://doc.rust-lang.org/stable/book/crates-and-modules.html) chapter if you
|
||||
haven't already, it will help you with organizing your files.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
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](https://github.com/orgs/exercism/teams/rust) are happy to help!
|
||||
|
||||
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).
|
||||
|
||||
[help-page]: http://exercism.io/languages/rust
|
||||
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html
|
||||
|
||||
## Source
|
||||
|
||||
Wikipedia [http://en.wikipedia.org/wiki/For_Want_of_a_Nail](http://en.wikipedia.org/wiki/For_Want_of_a_Nail)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
1
rust/proverb/src/lib.rs
Normal file
1
rust/proverb/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub fn build_proverb(list: Vec<&str>) -> String {
if list.len() == 0 { return String::from("") }
let last_line = if list.len() > 2 { String::from("And all for the want of a horseshoe nail.") }
else { String::from("And all for the want of a ") + list.first().unwrap() + &String::from(".") };
let mut return_vec: Vec<String> = Vec::new();
for i in 0..list.len()-1 {
return_vec.push(String::from("For want of a ") + list[i] + &String::from(" the ") + list[i+1] + &String::from(" was lost."))
}
return_vec.push(last_line);
return return_vec.join("\n")
}
|
48
rust/proverb/tests/proverb.rs
Normal file
48
rust/proverb/tests/proverb.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
extern crate proverb;
|
||||
|
||||
use proverb::build_proverb;
|
||||
|
||||
#[test]
|
||||
fn test_two_pieces() {
|
||||
let input = vec!["nail", "shoe"];
|
||||
let expected = vec!["For want of a nail the shoe was lost.",
|
||||
"And all for the want of a nail."].join("\n");
|
||||
assert_eq!(build_proverb(input), expected);
|
||||
}
|
||||
|
||||
// Notice the change in the last line at three pieces.
|
||||
#[test]
|
||||
fn test_three_pieces() {
|
||||
let input = vec!["nail", "shoe", "horse"];
|
||||
let expected = vec!["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 horseshoe nail."].join("\n");
|
||||
assert_eq!(build_proverb(input), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_one_piece() {
|
||||
let input = vec!["nail"];
|
||||
let expected = String::from("And all for the want of a nail.");
|
||||
assert_eq!(build_proverb(input), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_pieces() {
|
||||
let input: Vec<&str> = vec![];
|
||||
let expected = String::new();
|
||||
assert_eq!(build_proverb(input), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_full() {
|
||||
let input = vec!["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"];
|
||||
let expected = vec!["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 horseshoe nail."].join("\n");
|
||||
assert_eq!(build_proverb(input), expected);
|
||||
}
|
Loading…
Add table
Reference in a new issue