Skip to content

Commit cef5fa7

Browse files
authored
Merge pull request exercism#664 from Smarticles101/food-chain
food-chain: add to track
2 parents fadf388 + 38c9255 commit cef5fa7

6 files changed

Lines changed: 304 additions & 0 deletions

File tree

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@
291291

292292
]
293293
},
294+
{
295+
"slug": "food-chain",
296+
"difficulty": 6,
297+
"topics": []
298+
},
294299
{
295300
"slug": "beer-song",
296301
"difficulty": 6,

exercises/food-chain/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.stream.IntStream;
2+
3+
public class FoodChain {
4+
5+
private static final String[] ANIMALS = {
6+
"fly",
7+
"spider",
8+
"bird",
9+
"cat",
10+
"dog",
11+
"goat",
12+
"cow"
13+
};
14+
15+
private static final String[] FOLLOWUPS = {
16+
"It wriggled and jiggled and tickled inside her.",
17+
"How absurd to swallow a bird!",
18+
"Imagine that, to swallow a cat!",
19+
"What a hog, to swallow a dog!",
20+
"Just opened her throat and swallowed a goat!",
21+
"I don't know how she swallowed a cow!"
22+
};
23+
24+
public String verse(int verse) {
25+
return verseBeginning(verse) + verseMiddle(verse) + verseEnding(verse);
26+
}
27+
28+
public String verses(int startVerse, int endVerse) {
29+
30+
String[] verses = new String[endVerse - startVerse + 1];
31+
32+
for (int i = 0; i <= endVerse - startVerse; i++) {
33+
verses[i] = verse(i + 1);
34+
}
35+
36+
return String.join("\n\n", verses);
37+
}
38+
39+
private String verseBeginning(int verse) {
40+
41+
if (verse == 1) {
42+
return "I know an old lady who swallowed a fly.\n";
43+
}
44+
45+
if (verse == 8) {
46+
return "I know an old lady who swallowed a horse.\n";
47+
}
48+
49+
return "I know an old lady who swallowed a " + ANIMALS[verse - 1] + ".\n" + FOLLOWUPS[verse - 2] + "\n";
50+
}
51+
52+
private String verseMiddle(int verse) {
53+
54+
if (verse == 1 || verse == 8) {
55+
return "";
56+
}
57+
58+
String[] lines = new String[verse - 1];
59+
60+
for (int i = verse - 1; i >= 1; i--) {
61+
lines[lines.length - i] = "She swallowed the " +
62+
ANIMALS[i] + " to catch the " + ANIMALS[i - 1] +
63+
(i == 2 ? " that wriggled and jiggled and tickled inside her." : ".");
64+
}
65+
66+
return String.join("\n", lines) + "\n";
67+
}
68+
69+
private String verseEnding(int verse) {
70+
71+
if (verse == 8) {
72+
return "She's dead, of course!";
73+
}
74+
75+
return "I don't know why she swallowed the fly. Perhaps she'll die.";
76+
}
77+
}

exercises/food-chain/src/main/java/.keep

Whitespace-only changes.
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import org.junit.Before;
2+
import org.junit.Test;
3+
import org.junit.Ignore;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class FoodChainTest {
8+
private FoodChain foodChain;
9+
10+
@Before
11+
public void setup() {
12+
foodChain = new FoodChain();
13+
}
14+
15+
@Test
16+
public void fly() {
17+
int verse = 1;
18+
String expected = "I know an old lady who swallowed a fly.\n" +
19+
"I don't know why she swallowed the fly. Perhaps she'll die.";
20+
21+
assertEquals(expected, foodChain.verse(verse));
22+
}
23+
24+
@Test
25+
@Ignore("Remove to run test.")
26+
public void spider() {
27+
int verse = 2;
28+
String expected = "I know an old lady who swallowed a spider.\n" +
29+
"It wriggled and jiggled and tickled inside her.\n" +
30+
"She swallowed the spider to catch the fly.\n" +
31+
"I don't know why she swallowed the fly. Perhaps she'll die.";
32+
33+
assertEquals(expected, foodChain.verse(verse));
34+
}
35+
36+
@Test
37+
@Ignore("Remove to run test.")
38+
public void bird() {
39+
int verse = 3;
40+
String expected = "I know an old lady who swallowed a bird.\n" +
41+
"How absurd to swallow a bird!\n" +
42+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
43+
"She swallowed the spider to catch the fly.\n" +
44+
"I don't know why she swallowed the fly. Perhaps she'll die.";
45+
46+
assertEquals(expected, foodChain.verse(verse));
47+
}
48+
49+
@Test
50+
@Ignore("Remove to run test.")
51+
public void cat() {
52+
int verse = 4;
53+
String expected = "I know an old lady who swallowed a cat.\n" +
54+
"Imagine that, to swallow a cat!\n" +
55+
"She swallowed the cat to catch the bird.\n" +
56+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
57+
"She swallowed the spider to catch the fly.\n" +
58+
"I don't know why she swallowed the fly. Perhaps she'll die.";
59+
60+
assertEquals(expected, foodChain.verse(verse));
61+
}
62+
63+
64+
@Test
65+
@Ignore("Remove to run test.")
66+
public void dog() {
67+
int verse = 5;
68+
String expected = "I know an old lady who swallowed a dog.\n" +
69+
"What a hog, to swallow a dog!\n" +
70+
"She swallowed the dog to catch the cat.\n" +
71+
"She swallowed the cat to catch the bird.\n" +
72+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
73+
"She swallowed the spider to catch the fly.\n" +
74+
"I don't know why she swallowed the fly. Perhaps she'll die.";
75+
76+
assertEquals(expected, foodChain.verse(verse));
77+
}
78+
79+
@Test
80+
@Ignore("Remove to run test.")
81+
public void goat() {
82+
int verse = 6;
83+
String expected = "I know an old lady who swallowed a goat.\n" +
84+
"Just opened her throat and swallowed a goat!\n" +
85+
"She swallowed the goat to catch the dog.\n" +
86+
"She swallowed the dog to catch the cat.\n" +
87+
"She swallowed the cat to catch the bird.\n" +
88+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
89+
"She swallowed the spider to catch the fly.\n" +
90+
"I don't know why she swallowed the fly. Perhaps she'll die.";
91+
92+
assertEquals(expected, foodChain.verse(verse));
93+
}
94+
95+
@Test
96+
@Ignore("Remove to run test.")
97+
public void cow() {
98+
int verse = 7;
99+
String expected = "I know an old lady who swallowed a cow.\n" +
100+
"I don't know how she swallowed a cow!\n" +
101+
"She swallowed the cow to catch the goat.\n" +
102+
"She swallowed the goat to catch the dog.\n" +
103+
"She swallowed the dog to catch the cat.\n" +
104+
"She swallowed the cat to catch the bird.\n" +
105+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
106+
"She swallowed the spider to catch the fly.\n" +
107+
"I don't know why she swallowed the fly. Perhaps she'll die.";
108+
109+
assertEquals(expected, foodChain.verse(verse));
110+
}
111+
112+
@Test
113+
@Ignore("Remove to run test.")
114+
public void horse() {
115+
int verse = 8;
116+
String expected = "I know an old lady who swallowed a horse.\n" +
117+
"She's dead, of course!";
118+
119+
assertEquals(expected, foodChain.verse(verse));
120+
}
121+
122+
123+
@Test
124+
@Ignore("Remove to run test.")
125+
public void multipleVerses() {
126+
int startVerse = 1;
127+
int endVerse = 3;
128+
String expected = "I know an old lady who swallowed a fly.\n" +
129+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
130+
"\n" +
131+
"I know an old lady who swallowed a spider.\n" +
132+
"It wriggled and jiggled and tickled inside her.\n" +
133+
"She swallowed the spider to catch the fly.\n" +
134+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
135+
"\n" +
136+
"I know an old lady who swallowed a bird.\n" +
137+
"How absurd to swallow a bird!\n" +
138+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
139+
"She swallowed the spider to catch the fly.\n" +
140+
"I don't know why she swallowed the fly. Perhaps she'll die.";
141+
142+
assertEquals(expected, foodChain.verses(startVerse, endVerse));
143+
}
144+
145+
146+
@Test
147+
@Ignore("Remove to run test.")
148+
public void wholeSong() {
149+
int startVerse = 1;
150+
int endVerse = 8;
151+
String expected = "I know an old lady who swallowed a fly.\n" +
152+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
153+
"\n" +
154+
"I know an old lady who swallowed a spider.\n" +
155+
"It wriggled and jiggled and tickled inside her.\n" +
156+
"She swallowed the spider to catch the fly.\n" +
157+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
158+
"\n" +
159+
"I know an old lady who swallowed a bird.\n" +
160+
"How absurd to swallow a bird!\n" +
161+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
162+
"She swallowed the spider to catch the fly.\n" +
163+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
164+
"\n" +
165+
"I know an old lady who swallowed a cat.\n" +
166+
"Imagine that, to swallow a cat!\n" +
167+
"She swallowed the cat to catch the bird.\n" +
168+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
169+
"She swallowed the spider to catch the fly.\n" +
170+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
171+
"\n" +
172+
"I know an old lady who swallowed a dog.\n" +
173+
"What a hog, to swallow a dog!\n" +
174+
"She swallowed the dog to catch the cat.\n" +
175+
"She swallowed the cat to catch the bird.\n" +
176+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
177+
"She swallowed the spider to catch the fly.\n" +
178+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
179+
"\n" +
180+
"I know an old lady who swallowed a goat.\n" +
181+
"Just opened her throat and swallowed a goat!\n" +
182+
"She swallowed the goat to catch the dog.\n" +
183+
"She swallowed the dog to catch the cat.\n" +
184+
"She swallowed the cat to catch the bird.\n" +
185+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
186+
"She swallowed the spider to catch the fly.\n" +
187+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
188+
"\n" +
189+
"I know an old lady who swallowed a cow.\n" +
190+
"I don't know how she swallowed a cow!\n" +
191+
"She swallowed the cow to catch the goat.\n" +
192+
"She swallowed the goat to catch the dog.\n" +
193+
"She swallowed the dog to catch the cat.\n" +
194+
"She swallowed the cat to catch the bird.\n" +
195+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" +
196+
"She swallowed the spider to catch the fly.\n" +
197+
"I don't know why she swallowed the fly. Perhaps she'll die.\n" +
198+
"\n" +
199+
"I know an old lady who swallowed a horse.\n" +
200+
"She's dead, of course!";
201+
202+
assertEquals(expected, foodChain.verses(startVerse, endVerse));
203+
}
204+
}

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ include 'diamond'
2020
include 'difference-of-squares'
2121
include 'etl'
2222
include 'flatten-array'
23+
include 'food-chain'
2324
include 'gigasecond'
2425
include 'grade-school'
2526
include 'hamming'

0 commit comments

Comments
 (0)