Skip to content

Commit bb4388b

Browse files
authored
Add food-chain exercise (exercism#203)
1 parent 75892ee commit bb4388b

10 files changed

+417
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,14 @@
566566
"prerequisites": [],
567567
"difficulty": 4
568568
},
569+
{
570+
"slug": "food-chain",
571+
"name": "Food Chain",
572+
"uuid": "24277aaf-7ecb-47ce-b8a9-e309695d020f",
573+
"practices": [],
574+
"prerequisites": [],
575+
"difficulty": 4
576+
},
569577
{
570578
"slug": "simple-linked-list",
571579
"name": "Simple Linked List",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ignore
2+
3+
## Method Signatures in Wren
4+
5+
In wren, you can overload methods _by arity_.
6+
That means your class can have two (or more) methods with the **same name** as long as they accept different numbers of arguments.
7+
8+
See [Signatures][signatures] for more details.
9+
10+
[signatures]: https://wren.io/method-calls.html#signature
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+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"food-chain.wren"
8+
],
9+
"test": [
10+
"food-chain.spec.wren"
11+
],
12+
"example": [
13+
".meta/proof.ci.wren"
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,52 @@
1+
class FoodChain {
2+
/// Recite verse number `n`
3+
static recite(n) { new().verse(n - 1) }
4+
5+
/// Recite verses from number `from` to number `to` inclusive
6+
static recite(from, to) {
7+
var chain = new()
8+
var lines = []
9+
for (n in from..to) {
10+
lines.addAll(chain.verse(n - 1))
11+
lines.add("")
12+
}
13+
return lines[0...-1]
14+
}
15+
16+
construct new() {
17+
_animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]
18+
_phrases = {
19+
"spider": "It wriggled and jiggled and tickled inside her.",
20+
"bird": "How absurd to swallow a bird!",
21+
"cat": "Imagine that, to swallow a cat!",
22+
"dog": "What a hog, to swallow a dog!",
23+
"goat": "Just opened her throat and swallowed a goat!",
24+
"cow": "I don't know how she swallowed a cow!",
25+
"horse": "She's dead, of course!"
26+
}
27+
}
28+
29+
verse(i) {
30+
var lines = []
31+
var animal = _animals[i]
32+
lines.add("I know an old lady who swallowed a %(animal).")
33+
34+
if (animal != "fly") {
35+
lines.add(_phrases[animal])
36+
if (animal == "horse") {
37+
return lines
38+
}
39+
40+
var predator = animal
41+
for (j in i..1) {
42+
var prey = _animals[j - 1]
43+
var extra = prey == "spider" ? " that wriggled and jiggled and tickled inside her" : ""
44+
lines.add("She swallowed the %(predator) to catch the %(prey)%(extra).")
45+
predator = prey
46+
}
47+
}
48+
49+
lines.add("I don't know why she swallowed the fly. Perhaps she'll die.")
50+
return lines
51+
}
52+
}
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"

exercises/practice/food-chain/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)