Skip to content

Commit c88d6e2

Browse files
rdavid1099sjwarner-bp
authored andcommitted
proverb: add to track (exercism#1077)
* Initial commit for new exercise proverb * Add first test and source code passing it * Add second test with short chain of consequences * Add test for longer chain of events * Add test asserting dictionary is not hard coded * Add test for entire proverb * All tests and source code complete * Add stub due to low difficulty level * Add proverb to config.json and settings.gradle * Limit scope to package-private asccess level * Set config to unlock proverb by two-fer * Update tests to match exercism canonical data * Fix starter implementation to only include thrown exception * Add StringBuilder and basic refactoring * Fix test spacing * Refactor out extraneous if-statement * Add curly braces around if-statement
1 parent 901bc7a commit c88d6e2

7 files changed

Lines changed: 170 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@
200200
"unlocked_by": "rna-transcription",
201201
"uuid": "331073b3-bd1a-4868-b767-a64ce9fd9d97"
202202
},
203+
{
204+
"core": false,
205+
"difficulty": 4,
206+
"slug": "proverb",
207+
"topics": [
208+
"arrays",
209+
"loops",
210+
"strings"
211+
],
212+
"unlocked_by": "two-fer",
213+
"uuid": "9906491b-a638-408d-86a4-4ad320a92658"
214+
},
203215
{
204216
"core": false,
205217
"difficulty": 4,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
final class Proverb {
2+
private String[] words;
3+
4+
Proverb(String[] words) {
5+
this.words = words;
6+
}
7+
8+
String recite() {
9+
if (words.length < 1) {
10+
return "";
11+
}
12+
final StringBuilder result = new StringBuilder();
13+
for(int i = 1; i < words.length; i++) {
14+
result.append("For want of a " + words[i - 1] + " the " + words[i] + " was lost.\n");
15+
}
16+
result.append("And all for the want of a " + words[0] + ".");
17+
return result.toString();
18+
}
19+
}

‎exercises/proverb/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Proverb in Java
2+
3+
For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Dynamically output the full test of this proverbial rhyme.
4+
5+
> For want of a nail the shoe was lost.<br>
6+
> For want of a shoe the horse was lost.<br>
7+
> For want of a horse the rider was lost.<br>
8+
> For want of a rider the message was lost.<br>
9+
> For want of a message the battle was lost.<br>
10+
> For want of a battle the kingdom was lost.<br>
11+
> And all for the want of a horseshoe nail.<br>
12+
13+
# Running the tests
14+
15+
You can run all the tests for an exercise by entering
16+
17+
```sh
18+
$ gradle test
19+
```
20+
21+
in your terminal.
22+
23+
## Source
24+
25+
Wikipedia [https://en.wikipedia.org/wiki/For_Want_of_a_Nail](https://en.wikipedia.org/wiki/For_Want_of_a_Nail)
26+
27+
## Submitting Incomplete Solutions
28+
29+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

‎exercises/proverb/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Proverb {
2+
3+
Proverb(String[] words) {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
String recite() {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.junit.Assert.assertThat;
6+
7+
public class ProverbTest {
8+
9+
@Test
10+
public void zeroWordsAreGiven() {
11+
String[] words = new String[0];
12+
String proverb = new Proverb(words).recite(),
13+
expected = "";
14+
15+
assertThat(proverb, is(expected));
16+
}
17+
18+
@Ignore("Remove to run test")
19+
@Test
20+
public void singlePieceOfProverb() {
21+
String[] words = new String[]{"nail"};
22+
String proverb = new Proverb(words).recite(),
23+
expected = "And all for the want of a nail.";
24+
25+
assertThat(proverb, is(expected));
26+
}
27+
28+
@Ignore("Remove to run test")
29+
@Test
30+
public void twoPiecesOfProverb() {
31+
String[] words = new String[]{"nail", "shoe"};
32+
String proverb = new Proverb(words).recite(),
33+
expected = "For want of a nail the shoe was lost.\n" +
34+
"And all for the want of a nail.";
35+
36+
assertThat(proverb, is(expected));
37+
}
38+
39+
@Ignore("Remove to run test")
40+
@Test
41+
public void shortChainOfConsequences() {
42+
String[] words = new String[]{"nail", "shoe", "horse"};
43+
String proverb = new Proverb(words).recite(),
44+
expected = "For want of a nail the shoe was lost.\n" +
45+
"For want of a shoe the horse was lost.\n" +
46+
"And all for the want of a nail.";
47+
48+
assertThat(proverb, is(expected));
49+
}
50+
51+
@Ignore("Remove to run test")
52+
@Test
53+
public void fullProverb() {
54+
String[] words = new String[]{"nail", "shoe", "horse", "rider", "message", "battle", "kingdom"};
55+
String proverb = new Proverb(words).recite(),
56+
expected = "For want of a nail the shoe was lost.\n" +
57+
"For want of a shoe the horse was lost.\n" +
58+
"For want of a horse the rider was lost.\n" +
59+
"For want of a rider the message was lost.\n" +
60+
"For want of a message the battle was lost.\n" +
61+
"For want of a battle the kingdom was lost.\n" +
62+
"And all for the want of a nail.";
63+
64+
assertThat(proverb, is(expected));
65+
}
66+
67+
@Ignore("Remove to run test")
68+
@Test
69+
public void fourPiecesModernizedProverb() {
70+
String[] words = new String[]{"pin", "gun", "soldier", "battle"};
71+
String proverb = new Proverb(words).recite(),
72+
expected = "For want of a pin the gun was lost.\n" +
73+
"For want of a gun the soldier was lost.\n" +
74+
"For want of a soldier the battle was lost.\n" +
75+
"And all for the want of a pin.";
76+
77+
assertThat(proverb, is(expected));
78+
}
79+
80+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ include 'pig-latin'
5858
include 'poker'
5959
include 'prime-factors'
6060
include 'protein-translation'
61+
include 'proverb'
6162
include 'pythagorean-triplet'
6263
include 'queen-attack'
6364
include 'raindrops'

0 commit comments

Comments
 (0)