Skip to content

Commit 500b9a8

Browse files
committed
Add food-chain
1 parent 503e3da commit 500b9a8

10 files changed

Lines changed: 359 additions & 5 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@
450450
"prerequisites": [],
451451
"difficulty": 4
452452
},
453+
{
454+
"slug": "food-chain",
455+
"name": "Food Chain",
456+
"uuid": "e2a50db4-69bd-4d41-9756-5961510aeacc",
457+
"practices": [],
458+
"prerequisites": [],
459+
"difficulty": 4
460+
},
453461
{
454462
"slug": "grade-school",
455463
"name": "Grade School",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"food_chain.moon"
8+
],
9+
"test": [
10+
"food_chain_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
animals = {'fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse'}
2+
phrases = {
3+
fly: "I don't know why she swallowed the fly. Perhaps she'll die.",
4+
spider: 'It wriggled and jiggled and tickled inside her.',
5+
bird: 'How absurd to swallow a bird!',
6+
cat: 'Imagine that, to swallow a cat!',
7+
dog: 'What a hog, to swallow a dog!',
8+
goat: 'Just opened her throat and swallowed a goat!',
9+
cow: "I don't know how she swallowed a cow!",
10+
horse: "She's dead, of course!",
11+
}
12+
extra = {
13+
spider: ' that wriggled and jiggled and tickled inside her'
14+
}
15+
16+
predator = (i) -> animals[i]
17+
prey = (i) -> animals[i] .. (extra[animals[i]] or '')
18+
19+
verse = (n) ->
20+
animal = animals[n]
21+
lines = { "I know an old lady who swallowed a #{animal}." }
22+
lines[#lines + 1] = phrases[animal]
23+
if n > 1 and n < #animals
24+
for i = n, 2, -1
25+
lines[#lines + 1] = "She swallowed the #{predator i} to catch the #{prey i-1}."
26+
lines[#lines + 1] = phrases.fly
27+
table.concat lines, '\n'
28+
29+
{
30+
recite: (startVerse, endVerse) ->
31+
table.concat [verse i for i = startVerse, endVerse], '\n\n'
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
string_list = (list, level) ->
2+
if #list <= 2
3+
"{#{table.concat [quote elem for elem in *list], ', '}}"
4+
else
5+
lines = [indent quote(elem) .. ',', level + 1 for elem in *list]
6+
table.insert lines, 1, '{'
7+
table.insert lines, indent('}', level)
8+
table.concat lines, '\n'
9+
10+
{
11+
module_name: 'FoodChain',
12+
13+
generate_test: (case, level) ->
14+
lines = {
15+
"result = FoodChain.#{case.property} #{case.input.startVerse}, #{case.input.endVerse}",
16+
"expected = #{string_list case.expected, level}",
17+
"assert.are.equal table.concat(expected, \"\\n\"), result"
18+
}
19+
table.concat [indent line, level for line in *lines], '\n'
20+
}
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+
{
2+
recite: (startVerse, endVerse) ->
3+
error 'Implement me'
4+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
FoodChain = require 'food_chain'
2+
3+
describe 'food-chain', ->
4+
it 'fly', ->
5+
result = FoodChain.recite 1, 1
6+
expected = {'I know an old lady who swallowed a fly.', "I don't know why she swallowed the fly. Perhaps she'll die."}
7+
assert.are.equal table.concat(expected, "\n"), result
8+
9+
pending 'spider', ->
10+
result = FoodChain.recite 2, 2
11+
expected = {
12+
'I know an old lady who swallowed a spider.',
13+
'It wriggled and jiggled and tickled inside her.',
14+
'She swallowed the spider to catch the fly.',
15+
"I don't know why she swallowed the fly. Perhaps she'll die.",
16+
}
17+
assert.are.equal table.concat(expected, "\n"), result
18+
19+
pending 'bird', ->
20+
result = FoodChain.recite 3, 3
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+
}
28+
assert.are.equal table.concat(expected, "\n"), result
29+
30+
pending 'cat', ->
31+
result = FoodChain.recite 4, 4
32+
expected = {
33+
'I know an old lady who swallowed a cat.',
34+
'Imagine that, to swallow a cat!',
35+
'She swallowed the cat to catch the bird.',
36+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
37+
'She swallowed the spider to catch the fly.',
38+
"I don't know why she swallowed the fly. Perhaps she'll die.",
39+
}
40+
assert.are.equal table.concat(expected, "\n"), result
41+
42+
pending 'dog', ->
43+
result = FoodChain.recite 5, 5
44+
expected = {
45+
'I know an old lady who swallowed a dog.',
46+
'What a hog, to swallow a dog!',
47+
'She swallowed the dog to catch the cat.',
48+
'She swallowed the cat to catch the bird.',
49+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
50+
'She swallowed the spider to catch the fly.',
51+
"I don't know why she swallowed the fly. Perhaps she'll die.",
52+
}
53+
assert.are.equal table.concat(expected, "\n"), result
54+
55+
pending 'goat', ->
56+
result = FoodChain.recite 6, 6
57+
expected = {
58+
'I know an old lady who swallowed a goat.',
59+
'Just opened her throat and swallowed a goat!',
60+
'She swallowed the goat to catch the dog.',
61+
'She swallowed the dog to catch the cat.',
62+
'She swallowed the cat to catch the bird.',
63+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
64+
'She swallowed the spider to catch the fly.',
65+
"I don't know why she swallowed the fly. Perhaps she'll die.",
66+
}
67+
assert.are.equal table.concat(expected, "\n"), result
68+
69+
pending 'cow', ->
70+
result = FoodChain.recite 7, 7
71+
expected = {
72+
'I know an old lady who swallowed a cow.',
73+
"I don't know how she swallowed a cow!",
74+
'She swallowed the cow to catch the goat.',
75+
'She swallowed the goat to catch the dog.',
76+
'She swallowed the dog to catch the cat.',
77+
'She swallowed the cat to catch the bird.',
78+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
79+
'She swallowed the spider to catch the fly.',
80+
"I don't know why she swallowed the fly. Perhaps she'll die.",
81+
}
82+
assert.are.equal table.concat(expected, "\n"), result
83+
84+
pending 'horse', ->
85+
result = FoodChain.recite 8, 8
86+
expected = {'I know an old lady who swallowed a horse.', "She's dead, of course!"}
87+
assert.are.equal table.concat(expected, "\n"), result
88+
89+
pending 'multiple verses', ->
90+
result = FoodChain.recite 1, 3
91+
expected = {
92+
'I know an old lady who swallowed a fly.',
93+
"I don't know why she swallowed the fly. Perhaps she'll die.",
94+
'',
95+
'I know an old lady who swallowed a spider.',
96+
'It wriggled and jiggled and tickled inside her.',
97+
'She swallowed the spider to catch the fly.',
98+
"I don't know why she swallowed the fly. Perhaps she'll die.",
99+
'',
100+
'I know an old lady who swallowed a bird.',
101+
'How absurd to swallow a bird!',
102+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
103+
'She swallowed the spider to catch the fly.',
104+
"I don't know why she swallowed the fly. Perhaps she'll die.",
105+
}
106+
assert.are.equal table.concat(expected, "\n"), result
107+
108+
pending 'full song', ->
109+
result = FoodChain.recite 1, 8
110+
expected = {
111+
'I know an old lady who swallowed a fly.',
112+
"I don't know why she swallowed the fly. Perhaps she'll die.",
113+
'',
114+
'I know an old lady who swallowed a spider.',
115+
'It wriggled and jiggled and tickled inside her.',
116+
'She swallowed the spider to catch the fly.',
117+
"I don't know why she swallowed the fly. Perhaps she'll die.",
118+
'',
119+
'I know an old lady who swallowed a bird.',
120+
'How absurd to swallow a bird!',
121+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
122+
'She swallowed the spider to catch the fly.',
123+
"I don't know why she swallowed the fly. Perhaps she'll die.",
124+
'',
125+
'I know an old lady who swallowed a cat.',
126+
'Imagine that, to swallow a cat!',
127+
'She swallowed the cat to catch the bird.',
128+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
129+
'She swallowed the spider to catch the fly.',
130+
"I don't know why she swallowed the fly. Perhaps she'll die.",
131+
'',
132+
'I know an old lady who swallowed a dog.',
133+
'What a hog, to swallow a dog!',
134+
'She swallowed the dog to catch the cat.',
135+
'She swallowed the cat to catch the bird.',
136+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
137+
'She swallowed the spider to catch the fly.',
138+
"I don't know why she swallowed the fly. Perhaps she'll die.",
139+
'',
140+
'I know an old lady who swallowed a goat.',
141+
'Just opened her throat and swallowed a goat!',
142+
'She swallowed the goat to catch the dog.',
143+
'She swallowed the dog to catch the cat.',
144+
'She swallowed the cat to catch the bird.',
145+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
146+
'She swallowed the spider to catch the fly.',
147+
"I don't know why she swallowed the fly. Perhaps she'll die.",
148+
'',
149+
'I know an old lady who swallowed a cow.',
150+
"I don't know how she swallowed a cow!",
151+
'She swallowed the cow to catch the goat.',
152+
'She swallowed the goat to catch the dog.',
153+
'She swallowed the dog to catch the cat.',
154+
'She swallowed the cat to catch the bird.',
155+
'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.',
156+
'She swallowed the spider to catch the fly.',
157+
"I don't know why she swallowed the fly. Perhaps she'll die.",
158+
'',
159+
'I know an old lady who swallowed a horse.',
160+
"She's dead, of course!",
161+
}
162+
assert.are.equal table.concat(expected, "\n"), result

exercises/test_helpers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ Used in: anagram
5050
### Indented multi-line list of strings
5151

5252
```moonscript
53-
instruction_list = (list, level) ->
53+
string_list = (list, level) ->
5454
if #list <= 2
5555
"{#{table.concat [quote elem for elem in *list], ', '}}"
5656
else
57-
instrs = [indent quote(elem) .. ',', level + 1 for elem in *list]
58-
table.insert instrs, 1, '{'
59-
table.insert instrs, indent('}', level)
60-
table.concat instrs, '\n'
57+
lines = [indent quote(elem) .. ',', level + 1 for elem in *list]
58+
table.insert lines, 1, '{'
59+
table.insert lines, indent('}', level)
60+
table.concat lines, '\n'
6161
```
6262
The `indent` function is defined in bin/generate-spec
6363

0 commit comments

Comments
 (0)