Skip to content

Commit 79e30cd

Browse files
authored
Add food-chain (#373)
1 parent 2c657af commit 79e30cd

File tree

7 files changed

+335
-0
lines changed

7 files changed

+335
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@
201201
"prerequisites": [],
202202
"difficulty": 2
203203
},
204+
{
205+
"slug": "food-chain",
206+
"name": "Food Chain",
207+
"uuid": "32e9daaf-b58c-42a9-9b9b-8f54db58551f",
208+
"practices": [],
209+
"prerequisites": [],
210+
"difficulty": 2
211+
},
204212
{
205213
"slug": "gigasecond",
206214
"name": "Gigasecond",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Instructions
2+
3+
Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
5+
While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.
6+
7+
This is a [cumulative song][cumulative-song] of unknown origin.
8+
9+
This is one of many common variants.
10+
11+
```text
12+
I know an old lady who swallowed a fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.
14+
15+
I know an old lady who swallowed a spider.
16+
It wriggled and jiggled and tickled inside her.
17+
She swallowed the spider to catch the fly.
18+
I don't know why she swallowed the fly. Perhaps she'll die.
19+
20+
I know an old lady who swallowed a bird.
21+
How absurd to swallow a bird!
22+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
23+
She swallowed the spider to catch the fly.
24+
I don't know why she swallowed the fly. Perhaps she'll die.
25+
26+
I know an old lady who swallowed a cat.
27+
Imagine that, to swallow a cat!
28+
She swallowed the cat to catch the bird.
29+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
30+
She swallowed the spider to catch the fly.
31+
I don't know why she swallowed the fly. Perhaps she'll die.
32+
33+
I know an old lady who swallowed a dog.
34+
What a hog, to swallow a dog!
35+
She swallowed the dog to catch the cat.
36+
She swallowed the cat to catch the bird.
37+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
38+
She swallowed the spider to catch the fly.
39+
I don't know why she swallowed the fly. Perhaps she'll die.
40+
41+
I know an old lady who swallowed a goat.
42+
Just opened her throat and swallowed a goat!
43+
She swallowed the goat to catch the dog.
44+
She swallowed the dog to catch the cat.
45+
She swallowed the cat to catch the bird.
46+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
47+
She swallowed the spider to catch the fly.
48+
I don't know why she swallowed the fly. Perhaps she'll die.
49+
50+
I know an old lady who swallowed a cow.
51+
I don't know how she swallowed a cow!
52+
She swallowed the cow to catch the goat.
53+
She swallowed the goat to catch the dog.
54+
She swallowed the dog to catch the cat.
55+
She swallowed the cat to catch the bird.
56+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
57+
She swallowed the spider to catch the fly.
58+
I don't know why she swallowed the fly. Perhaps she'll die.
59+
60+
I know an old lady who swallowed a horse.
61+
She's dead, of course!
62+
```
63+
64+
[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"food-chain.coffee"
8+
],
9+
"test": [
10+
"food-chain.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class FoodChain
2+
@chain = [
3+
{ animal: "fly", line: "I don't know why she swallowed the fly. Perhaps she'll die." }
4+
{ animal: "spider", line: "It wriggled and jiggled and tickled inside her." }
5+
{ animal: "bird", line: "How absurd to swallow a bird!" }
6+
{ animal: "cat", line: "Imagine that, to swallow a cat!" }
7+
{ animal: "dog", line: "What a hog, to swallow a dog!" }
8+
{ animal: "goat", line: "Just opened her throat and swallowed a goat!" }
9+
{ animal: "cow", line: "I don't know how she swallowed a cow!" }
10+
{ animal: "horse", line: "She's dead, of course!" }
11+
]
12+
13+
@recite: (startVerse, endVerse) ->
14+
result = []
15+
for verse in [startVerse..endVerse]
16+
lines = []
17+
{animal, line} = @chain[verse - 1]
18+
lines.push "I know an old lady who swallowed a #{animal}."
19+
lines.push line
20+
21+
if verse is 8
22+
result.push lines.join("\n")
23+
continue
24+
25+
if verse > 1
26+
for i in [verse - 1..1]
27+
current = @chain[i]
28+
previous = @chain[i - 1]
29+
if i is 2
30+
lines.push "She swallowed the #{current.animal} to catch the #{previous.animal} that wriggled and jiggled and tickled inside her."
31+
else
32+
lines.push "She swallowed the #{current.animal} to catch the #{previous.animal}."
33+
lines.push "I don't know why she swallowed the fly. Perhaps she'll die."
34+
35+
if verse < endVerse
36+
lines.push ""
37+
38+
result.push lines.join "\n"
39+
result.join "\n"
40+
41+
module.exports = FoodChain
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[751dce68-9412-496e-b6e8-855998c56166]
13+
description = "fly"
14+
15+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
16+
description = "spider"
17+
18+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
19+
description = "bird"
20+
21+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
22+
description = "cat"
23+
24+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
25+
description = "dog"
26+
27+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
28+
description = "goat"
29+
30+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
31+
description = "cow"
32+
33+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
34+
description = "horse"
35+
36+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
37+
description = "multiple verses"
38+
39+
[e626b32b-745c-4101-bcbd-3b13456893db]
40+
description = "full song"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class FoodChain
2+
@recite: (startVerse, endVerse) ->
3+
4+
module.exports = FoodChain
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
FoodChain = require './food-chain'
2+
3+
describe 'Food Chain', ->
4+
it 'fly', ->
5+
expected = [
6+
"I know an old lady who swallowed a fly."
7+
"I don't know why she swallowed the fly. Perhaps she'll die."
8+
].join "\n"
9+
expect(FoodChain.recite 1, 1).toEqual expected
10+
11+
xit 'spider', ->
12+
expected = [
13+
"I know an old lady who swallowed a spider."
14+
"It wriggled and jiggled and tickled inside her."
15+
"She swallowed the spider to catch the fly."
16+
"I don't know why she swallowed the fly. Perhaps she'll die."
17+
].join "\n"
18+
expect(FoodChain.recite 2, 2).toEqual expected
19+
20+
xit 'bird', ->
21+
expected = [
22+
"I know an old lady who swallowed a bird."
23+
"How absurd to swallow a bird!"
24+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
25+
"She swallowed the spider to catch the fly."
26+
"I don't know why she swallowed the fly. Perhaps she'll die."
27+
].join "\n"
28+
expect(FoodChain.recite 3, 3).toEqual expected
29+
30+
xit 'cat', ->
31+
expected = [
32+
"I know an old lady who swallowed a cat."
33+
"Imagine that, to swallow a cat!"
34+
"She swallowed the cat to catch the bird."
35+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
36+
"She swallowed the spider to catch the fly."
37+
"I don't know why she swallowed the fly. Perhaps she'll die."
38+
].join "\n"
39+
expect(FoodChain.recite 4, 4).toEqual expected
40+
41+
xit 'dog', ->
42+
expected = [
43+
"I know an old lady who swallowed a dog."
44+
"What a hog, to swallow a dog!"
45+
"She swallowed the dog to catch the cat."
46+
"She swallowed the cat to catch the bird."
47+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
48+
"She swallowed the spider to catch the fly."
49+
"I don't know why she swallowed the fly. Perhaps she'll die."
50+
].join "\n"
51+
expect(FoodChain.recite 5, 5).toEqual expected
52+
53+
xit 'goat', ->
54+
expected = [
55+
"I know an old lady who swallowed a goat."
56+
"Just opened her throat and swallowed a goat!"
57+
"She swallowed the goat to catch the dog."
58+
"She swallowed the dog to catch the cat."
59+
"She swallowed the cat to catch the bird."
60+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
61+
"She swallowed the spider to catch the fly."
62+
"I don't know why she swallowed the fly. Perhaps she'll die."
63+
].join "\n"
64+
expect(FoodChain.recite 6, 6).toEqual expected
65+
66+
xit 'cow', ->
67+
expected = [
68+
"I know an old lady who swallowed a cow."
69+
"I don't know how she swallowed a cow!"
70+
"She swallowed the cow to catch the goat."
71+
"She swallowed the goat to catch the dog."
72+
"She swallowed the dog to catch the cat."
73+
"She swallowed the cat to catch the bird."
74+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
75+
"She swallowed the spider to catch the fly."
76+
"I don't know why she swallowed the fly. Perhaps she'll die."
77+
].join "\n"
78+
expect(FoodChain.recite 7, 7).toEqual expected
79+
80+
xit 'horse', ->
81+
expected = [
82+
"I know an old lady who swallowed a horse."
83+
"She's dead, of course!"
84+
].join "\n"
85+
expect(FoodChain.recite 8, 8).toEqual expected
86+
87+
xit 'multiple verses', ->
88+
expected = [
89+
"I know an old lady who swallowed a fly."
90+
"I don't know why she swallowed the fly. Perhaps she'll die."
91+
""
92+
"I know an old lady who swallowed a spider."
93+
"It wriggled and jiggled and tickled inside her."
94+
"She swallowed the spider to catch the fly."
95+
"I don't know why she swallowed the fly. Perhaps she'll die."
96+
""
97+
"I know an old lady who swallowed a bird."
98+
"How absurd to swallow a bird!"
99+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
100+
"She swallowed the spider to catch the fly."
101+
"I don't know why she swallowed the fly. Perhaps she'll die."
102+
].join "\n"
103+
expect(FoodChain.recite 1, 3).toEqual expected
104+
105+
xit 'full song', ->
106+
expected = [
107+
"I know an old lady who swallowed a fly."
108+
"I don't know why she swallowed the fly. Perhaps she'll die."
109+
""
110+
"I know an old lady who swallowed a spider."
111+
"It wriggled and jiggled and tickled inside her."
112+
"She swallowed the spider to catch the fly."
113+
"I don't know why she swallowed the fly. Perhaps she'll die."
114+
""
115+
"I know an old lady who swallowed a bird."
116+
"How absurd to swallow a bird!"
117+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
118+
"She swallowed the spider to catch the fly."
119+
"I don't know why she swallowed the fly. Perhaps she'll die."
120+
""
121+
"I know an old lady who swallowed a cat."
122+
"Imagine that, to swallow a cat!"
123+
"She swallowed the cat to catch the bird."
124+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
125+
"She swallowed the spider to catch the fly."
126+
"I don't know why she swallowed the fly. Perhaps she'll die."
127+
""
128+
"I know an old lady who swallowed a dog."
129+
"What a hog, to swallow a dog!"
130+
"She swallowed the dog to catch the cat."
131+
"She swallowed the cat to catch the bird."
132+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
133+
"She swallowed the spider to catch the fly."
134+
"I don't know why she swallowed the fly. Perhaps she'll die."
135+
""
136+
"I know an old lady who swallowed a goat."
137+
"Just opened her throat and swallowed a goat!"
138+
"She swallowed the goat to catch the dog."
139+
"She swallowed the dog to catch the cat."
140+
"She swallowed the cat to catch the bird."
141+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
142+
"She swallowed the spider to catch the fly."
143+
"I don't know why she swallowed the fly. Perhaps she'll die."
144+
""
145+
"I know an old lady who swallowed a cow."
146+
"I don't know how she swallowed a cow!"
147+
"She swallowed the cow to catch the goat."
148+
"She swallowed the goat to catch the dog."
149+
"She swallowed the dog to catch the cat."
150+
"She swallowed the cat to catch the bird."
151+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
152+
"She swallowed the spider to catch the fly."
153+
"I don't know why she swallowed the fly. Perhaps she'll die."
154+
""
155+
"I know an old lady who swallowed a horse."
156+
"She's dead, of course!"
157+
].join "\n"
158+
expect(FoodChain.recite 1, 8).toEqual expected
159+

0 commit comments

Comments
 (0)