Skip to content

Commit 4dc76ef

Browse files
Add food-chain exercise (#243)
1 parent 3cf03df commit 4dc76ef

File tree

7 files changed

+325
-0
lines changed

7 files changed

+325
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,14 @@
631631
"prerequisites": [],
632632
"difficulty": 5
633633
},
634+
{
635+
"slug": "food-chain",
636+
"name": "Food Chain",
637+
"uuid": "66524c69-1ab9-445f-99f3-ebfdb1b39613",
638+
"practices": [],
639+
"prerequisites": [],
640+
"difficulty": 5
641+
},
634642
{
635643
"slug": "grade-school",
636644
"name": "Grade School",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"food-chain.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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,50 @@
1+
module main
2+
3+
import strings
4+
5+
const animals = ['', 'fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse']
6+
7+
const exclamations = [
8+
'',
9+
"I don't know why she swallowed the fly. Perhaps she'll die.\n\n",
10+
'It wriggled and jiggled and tickled inside her.\n',
11+
'How absurd to swallow a bird!\n',
12+
'Imagine that, to swallow a cat!\n',
13+
'What a hog, to swallow a dog!\n',
14+
'Just opened her throat and swallowed a goat!\n',
15+
"I don't know how she swallowed a cow!\n",
16+
"She's dead, of course!\n\n",
17+
]
18+
19+
fn recite(start_verse int, end_verse int) string {
20+
mut builder := strings.new_builder(4000)
21+
for verse in start_verse .. (end_verse + 1) {
22+
builder.write_string('I know an old lady who swallowed a ')
23+
builder.write_string(animals[verse])
24+
builder.write_string('.\n')
25+
builder.write_string(exclamations[verse])
26+
27+
if verse == 1 || verse == 8 {
28+
continue
29+
}
30+
31+
mut animal := verse
32+
for animal > 1 {
33+
builder.write_string('She swallowed the ')
34+
builder.write_string(animals[animal])
35+
builder.write_string(' to catch the ')
36+
37+
animal--
38+
builder.write_string(animals[animal])
39+
40+
if animal == 2 {
41+
builder.write_string(' that wriggled and jiggled and tickled inside her')
42+
}
43+
44+
builder.write_string('.\n')
45+
}
46+
builder.write_string(exclamations[1])
47+
}
48+
builder.go_back(2) // Omit final newlines
49+
return builder.str()
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[751dce68-9412-496e-b6e8-855998c56166]
6+
description = "fly"
7+
8+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
9+
description = "spider"
10+
11+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
12+
description = "bird"
13+
14+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
15+
description = "cat"
16+
17+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
18+
description = "dog"
19+
20+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
21+
description = "goat"
22+
23+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
24+
description = "cow"
25+
26+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
27+
description = "horse"
28+
29+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
30+
description = "multiple verses"
31+
32+
[e626b32b-745c-4101-bcbd-3b13456893db]
33+
description = "full song"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn recite(start_verse int, end_verse int) string {
4+
}
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
module main
2+
3+
fn test_fly() {
4+
expected := ("I know an old lady who swallowed a fly.
5+
I don't know why she swallowed the fly. Perhaps she'll die.")
6+
assert recite(1, 1) == expected
7+
}
8+
9+
fn test_spider() {
10+
expected := ("I know an old lady who swallowed a spider.
11+
It wriggled and jiggled and tickled inside her.
12+
She swallowed the spider to catch the fly.
13+
I don't know why she swallowed the fly. Perhaps she'll die.")
14+
assert recite(2, 2) == expected
15+
}
16+
17+
fn test_bird() {
18+
expected := ("I know an old lady who swallowed a bird.
19+
How absurd to swallow a bird!
20+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
21+
She swallowed the spider to catch the fly.
22+
I don't know why she swallowed the fly. Perhaps she'll die.")
23+
assert recite(3, 3) == expected
24+
}
25+
26+
fn test_cat() {
27+
expected := ("I know an old lady who swallowed a cat.
28+
Imagine that, to swallow a cat!
29+
She swallowed the cat to catch the bird.
30+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
31+
She swallowed the spider to catch the fly.
32+
I don't know why she swallowed the fly. Perhaps she'll die.")
33+
assert recite(4, 4) == expected
34+
}
35+
36+
fn test_dog() {
37+
expected := ("I know an old lady who swallowed a dog.
38+
What a hog, to swallow a dog!
39+
She swallowed the dog to catch the cat.
40+
She swallowed the cat to catch the bird.
41+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
42+
She swallowed the spider to catch the fly.
43+
I don't know why she swallowed the fly. Perhaps she'll die.")
44+
assert recite(5, 5) == expected
45+
}
46+
47+
fn test_goat() {
48+
expected := ("I know an old lady who swallowed a goat.
49+
Just opened her throat and swallowed a goat!
50+
She swallowed the goat to catch the dog.
51+
She swallowed the dog to catch the cat.
52+
She swallowed the cat to catch the bird.
53+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
54+
She swallowed the spider to catch the fly.
55+
I don't know why she swallowed the fly. Perhaps she'll die.")
56+
assert recite(6, 6) == expected
57+
}
58+
59+
fn test_cow() {
60+
expected := ("I know an old lady who swallowed a cow.
61+
I don't know how she swallowed a cow!
62+
She swallowed the cow to catch the goat.
63+
She swallowed the goat to catch the dog.
64+
She swallowed the dog to catch the cat.
65+
She swallowed the cat to catch the bird.
66+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
67+
She swallowed the spider to catch the fly.
68+
I don't know why she swallowed the fly. Perhaps she'll die.")
69+
assert recite(7, 7) == expected
70+
}
71+
72+
fn test_horse() {
73+
expected := ("I know an old lady who swallowed a horse.
74+
She's dead, of course!")
75+
assert recite(8, 8) == expected
76+
}
77+
78+
fn test_multiple_verses() {
79+
expected := ("I know an old lady who swallowed a fly.
80+
I don't know why she swallowed the fly. Perhaps she'll die.
81+
82+
I know an old lady who swallowed a spider.
83+
It wriggled and jiggled and tickled inside her.
84+
She swallowed the spider to catch the fly.
85+
I don't know why she swallowed the fly. Perhaps she'll die.
86+
87+
I know an old lady who swallowed a bird.
88+
How absurd to swallow a bird!
89+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
90+
She swallowed the spider to catch the fly.
91+
I don't know why she swallowed the fly. Perhaps she'll die.")
92+
assert recite(1, 3) == expected
93+
}
94+
95+
fn test_full_song() {
96+
expected := ("I know an old lady who swallowed a fly.
97+
I don't know why she swallowed the fly. Perhaps she'll die.
98+
99+
I know an old lady who swallowed a spider.
100+
It wriggled and jiggled and tickled inside her.
101+
She swallowed the spider to catch the fly.
102+
I don't know why she swallowed the fly. Perhaps she'll die.
103+
104+
I know an old lady who swallowed a bird.
105+
How absurd to swallow a bird!
106+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
107+
She swallowed the spider to catch the 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 cat.
111+
Imagine that, to swallow a cat!
112+
She swallowed the cat to catch the bird.
113+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
114+
She swallowed the spider to catch the fly.
115+
I don't know why she swallowed the fly. Perhaps she'll die.
116+
117+
I know an old lady who swallowed a dog.
118+
What a hog, to swallow a dog!
119+
She swallowed the dog to catch the cat.
120+
She swallowed the cat to catch the 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 goat.
126+
Just opened her throat and swallowed a goat!
127+
She swallowed the goat to catch the dog.
128+
She swallowed the dog to catch the cat.
129+
She swallowed the cat to catch the bird.
130+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
131+
She swallowed the spider to catch the fly.
132+
I don't know why she swallowed the fly. Perhaps she'll die.
133+
134+
I know an old lady who swallowed a cow.
135+
I don't know how she swallowed a cow!
136+
She swallowed the cow to catch the goat.
137+
She swallowed the goat to catch the dog.
138+
She swallowed the dog to catch the cat.
139+
She swallowed the cat to catch the bird.
140+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
141+
She swallowed the spider to catch the fly.
142+
I don't know why she swallowed the fly. Perhaps she'll die.
143+
144+
I know an old lady who swallowed a horse.
145+
She's dead, of course!")
146+
assert recite(1, 8) == expected
147+
}

0 commit comments

Comments
 (0)