Skip to content

Commit 694bf71

Browse files
authored
Add food-chain exercise (#323)
1 parent fd6a691 commit 694bf71

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,14 @@
712712
"practices": [],
713713
"prerequisites": [],
714714
"difficulty": 6
715+
},
716+
{
717+
"slug": "food-chain",
718+
"name": "Food Chain",
719+
"uuid": "adf3945f-6735-471c-b02b-b3dac0aab1bc",
720+
"practices": [],
721+
"prerequisites": [],
722+
"difficulty": 3
715723
}
716724
],
717725
"foregone": [
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"kahgoh"
4+
],
5+
"files": {
6+
"solution": [
7+
"food_chain.vim"
8+
],
9+
"test": [
10+
"food_chain.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
let creatures=['fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse']
2+
3+
let uniqueLine={
4+
\ 'fly': "I don't know why she swallowed the fly. Perhaps she'll die.",
5+
\ 'spider': 'It wriggled and jiggled and tickled inside her.',
6+
\ 'bird': 'How absurd to swallow a bird!',
7+
\ 'cat': 'Imagine that, to swallow a cat!',
8+
\ 'dog': 'What a hog, to swallow a dog!',
9+
\ 'goat': 'Just opened her throat and swallowed a goat!',
10+
\ 'cow': "I don't know how she swallowed a cow!",
11+
\ 'horse': "She's dead, of course!"
12+
\ }
13+
14+
function! Verse(num)
15+
let i = a:num
16+
let creature = g:creatures[l:i - 1]
17+
let lines = [
18+
\ 'I know an old lady who swallowed a ' . l:creature . '.',
19+
\ g:uniqueLine[l:creature]
20+
\ ]
21+
22+
if l:creature ==# 'fly' || l:creature ==# 'horse'
23+
return l:lines
24+
endif
25+
26+
while l:i > 1
27+
let creature1 = g:creatures[l:i - 1]
28+
let creature2 = g:creatures[l:i - 2]
29+
if l:creature2 ==# 'spider'
30+
let lines = add(l:lines, 'She swallowed the ' . creature1 . ' to catch the ' . creature2 . ' that wriggled and jiggled and tickled inside her.')
31+
else
32+
let lines = add(l:lines, 'She swallowed the ' . creature1 . ' to catch the ' . creature2 . '.')
33+
endif
34+
let i -= 1
35+
endwhile
36+
37+
return add(l:lines, g:uniqueLine['fly'])
38+
endfunction
39+
40+
function! Recite(endVerse, startVerse)
41+
let content=[]
42+
let num = a:startVerse
43+
let break = 0
44+
45+
while l:num <= a:endVerse
46+
if l:break
47+
let content = add(l:content, '')
48+
endif
49+
let content += Verse(l:num)
50+
51+
let num += 1
52+
let break = 1
53+
endwhile
54+
55+
return content
56+
endfunction
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
Execute (fly):
3+
let g:endVerse = 1
4+
let g:startVerse = 1
5+
let g:expected = ['I know an old lady who swallowed a fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
6+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
7+
8+
Execute (spider):
9+
let g:endVerse = 2
10+
let g:startVerse = 2
11+
let g:expected = ['I know an old lady who swallowed a spider.', 'It wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
12+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
13+
14+
Execute (bird):
15+
let g:endVerse = 3
16+
let g:startVerse = 3
17+
let g:expected = ['I know an old lady who swallowed a bird.', 'How absurd to swallow a bird!', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
18+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
19+
20+
Execute (cat):
21+
let g:endVerse = 4
22+
let g:startVerse = 4
23+
let g:expected = ['I know an old lady who swallowed a cat.', 'Imagine that, to swallow a cat!', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
24+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
25+
26+
Execute (dog):
27+
let g:endVerse = 5
28+
let g:startVerse = 5
29+
let g:expected = ['I know an old lady who swallowed a dog.', 'What a hog, to swallow a dog!', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
30+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
31+
32+
Execute (goat):
33+
let g:endVerse = 6
34+
let g:startVerse = 6
35+
let g:expected = ['I know an old lady who swallowed a goat.', 'Just opened her throat and swallowed a goat!', 'She swallowed the goat to catch the dog.', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
36+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
37+
38+
Execute (cow):
39+
let g:endVerse = 7
40+
let g:startVerse = 7
41+
let g:expected = ['I know an old lady who swallowed a cow.', 'I don''t know how she swallowed a cow!', 'She swallowed the cow to catch the goat.', 'She swallowed the goat to catch the dog.', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
42+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
43+
44+
Execute (horse):
45+
let g:endVerse = 8
46+
let g:startVerse = 8
47+
let g:expected = ['I know an old lady who swallowed a horse.', 'She''s dead, of course!']
48+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
49+
50+
Execute (multiple verses):
51+
let g:endVerse = 3
52+
let g:startVerse = 1
53+
let g:expected = ['I know an old lady who swallowed a fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a spider.', 'It wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a bird.', 'How absurd to swallow a bird!', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.']
54+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
55+
56+
Execute (full song):
57+
let g:endVerse = 8
58+
let g:startVerse = 1
59+
let g:expected = ['I know an old lady who swallowed a fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a spider.', 'It wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a bird.', 'How absurd to swallow a bird!', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a cat.', 'Imagine that, to swallow a cat!', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a dog.', 'What a hog, to swallow a dog!', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a goat.', 'Just opened her throat and swallowed a goat!', 'She swallowed the goat to catch the dog.', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a cow.', 'I don''t know how she swallowed a cow!', 'She swallowed the cow to catch the goat.', 'She swallowed the goat to catch the dog.', 'She swallowed the dog to catch the cat.', 'She swallowed the cat to catch the bird.', 'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.', 'She swallowed the spider to catch the fly.', 'I don''t know why she swallowed the fly. Perhaps she''ll die.', '', 'I know an old lady who swallowed a horse.', 'She''s dead, of course!']
60+
AssertEqual g:expected, Recite(g:endVerse, g:startVerse)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"
2+
" Return a list containing the lyrics to `I Know an Old Lady Who Swallowed a Fly`,
3+
" using a verse number to end on and another to start from
4+
"
5+
function! Recite(endVerse, startVerse)
6+
" your solution goes here
7+
endfunction

0 commit comments

Comments
 (0)