Skip to content

Commit

Permalink
Add food-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jan 20, 2025
1 parent 4ed4430 commit 42f6d97
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "2ebe1be9-7cd5-4fcc-a420-fee670a7a4b0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "house",
"name": "House",
Expand Down
64 changes: 64 additions & 0 deletions exercises/practice/food-chain/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Instructions

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a [cumulative song][cumulative-song] of unknown origin.

This is one of many common variants.

```text
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!
```

[cumulative-song]: https://en.wikipedia.org/wiki/Cumulative_song
19 changes: 19 additions & 0 deletions exercises/practice/food-chain/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/food-chain.art"
],
"test": [
"tests/test-food-chain.art"
],
"example": [
".meta/src/example.art"
]
},
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
}
35 changes: 35 additions & 0 deletions exercises/practice/food-chain/.meta/src/example.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
foodChain: [
["fly" "I don't know why she swallowed the fly. Perhaps she'll die."]
["spider" "It wriggled and jiggled and tickled inside her."]
["bird" "How absurd to swallow a bird!"]
["cat" "Imagine that, to swallow a cat!"]
["dog" "What a hog, to swallow a dog!"]
["goat" "Just opened her throat and swallowed a goat!"]
["cow" "I don't know how she swallowed a cow!"]
["horse" "She's dead, of course!"]
]

recite: function [start end][
join.with:"\n" fold.seed:[] start..end [acc verse] [
lines: []
[animal comment]: foodChain\[dec verse]

lines: @[~"I know an old lady who swallowed a |animal|." comment]
if verse = 8 -> return join.with:"\n" acc ++ join.with:"\n" lines
if verse > 1 [
loop range dec verse 1 'i [
current: foodChain\[i]\0
previous: foodChain\[dec i]\0
case i [
2 -> 'lines ++ ~"She swallowed the |current| to catch the |previous| that wriggled and jiggled and tickled inside her."
any -> 'lines ++ ~"She swallowed the |current| to catch the |previous|."
]
]
'lines ++ "I don't know why she swallowed the fly. Perhaps she'll die."
]

if verse < end -> 'lines ++ ""

acc ++ join.with:"\n" lines
]
]
40 changes: 40 additions & 0 deletions exercises/practice/food-chain/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[751dce68-9412-496e-b6e8-855998c56166]
description = "fly"

[6c56f861-0c5e-4907-9a9d-b2efae389379]
description = "spider"

[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
description = "bird"

[e866a758-e1ff-400e-9f35-f27f28cc288f]
description = "cat"

[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
description = "dog"

[4b3fd221-01ea-46e0-825b-5734634fbc59]
description = "goat"

[1b707da9-7001-4fac-941f-22ad9c7a65d4]
description = "cow"

[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
description = "horse"

[22b863d5-17e4-4d1e-93e4-617329a5c050]
description = "multiple verses"

[e626b32b-745c-4101-bcbd-3b13456893db]
description = "full song"
3 changes: 3 additions & 0 deletions exercises/practice/food-chain/src/food-chain.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recite: function [startVerse endVerse][
panic "Please implement the recite function"
]
3 changes: 3 additions & 0 deletions exercises/practice/food-chain/tester.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {unitt}!

runTests.failFast findTests "tests"
181 changes: 181 additions & 0 deletions exercises/practice/food-chain/tests/test-food-chain.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import {unitt}!
import {src/food-chain}!


suite "Food Chain" [
test "fly" [
results: recite 1 1
expected: join.with:"\n" [
"I know an old lady who swallowed a fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
assert -> expected = results
]

test.skip "spider" [
results: recite 2 2
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "bird" [
results: recite 3 3
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "cat" [
results: recite 4 4
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "dog" [
results: recite 5 5
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "goat" [
results: recite 6 6
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "cow" [
results: recite 7 7
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "horse" [
results: recite 8 8
expected: join.with:"\n" [
"I know an old lady who swallowed a horse."
"She's dead, of course!"
]
assert -> expected = results
]

test.skip "multiple verses" [
results: recite 1 3
expected: join.with:"\n" [
"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."
]
assert -> expected = results
]

test.skip "full song" [
results: recite 1 8
expected: join.with:"\n" [
"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!"
]
assert -> expected = results
]
]

0 comments on commit 42f6d97

Please sign in to comment.