Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add practice exercise food-chain #770

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "dacbf1b1-586d-4f71-9941-c0cd36090264",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
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": [
"colinleach"
],
"files": {
"solution": [
"food-chain.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"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"
}
34 changes: 34 additions & 0 deletions exercises/practice/food-chain/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const ANIMALS = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]
const LINE2 = ["",
"It wriggled and jiggled and tickled inside her.",
"How absurd to swallow a bird!",
"Imagine that, to swallow a cat!",
"What a hog, to swallow a dog!",
"Just opened her throat and swallowed a goat!",
"I don't know how she swallowed a cow!",
"She's dead, of course!"]

const WRIGGLE = " that wriggled and jiggled and tickled inside her."
const LAST = "I don't know why she swallowed the fly. Perhaps she'll die."

function recite(start_verse, end_verse)
verses = [make_verse(i) for i in start_verse:end_verse]
all_verses = [line for v in verses for line in v]
all_verses[2:end]
end

function make_verse(n)
verse = ["", "I know an old lady who swallowed a $(ANIMALS[n])."]
isempty(LINE2[n]) || push!(verse, LINE2[n])

ANIMALS[n] == "horse" && return verse

for v in n:-1:2
line = "She swallowed the $(ANIMALS[v]) to catch the $(ANIMALS[v-1])"
line *= ANIMALS[v - 1] == "spider" ? WRIGGLE : "."
push!(verse, line)
end

push!(verse, LAST)
verse
end
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/food-chain.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function recite(start_verse, end_verse)

end
172 changes: 172 additions & 0 deletions exercises/practice/food-chain/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using Test

include("food-chain.jl")

@testset verbose = true "tests" begin

@testset "fly" begin
expected = [
"I know an old lady who swallowed a fly.",
"I don't know why she swallowed the fly. Perhaps she'll die."
]
@test recite(1, 1) == expected
end

@testset "spider" begin
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.",
]
@test recite(2, 2) == expected
end

@testset "bird" begin
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.",
]
@test recite(3, 3) == expected
end

@testset "cat" begin
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.",
]
@test recite(4, 4) == expected
end

@testset "dog" begin
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.",
]
@test recite(5, 5) == expected
end

@testset "goat" begin
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.",
]
@test recite(6, 6) == expected
end

@testset "cow" begin
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.",
]
@test recite(7, 7) == expected
end

@testset "horse" begin
expected = [
"I know an old lady who swallowed a horse.", "She's dead, of course!",
]
@test recite(8, 8) == expected
end

@testset "multiple verses" begin
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.",
]
@test recite(1, 3) == expected
end

@testset "full song" begin
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!",
]
@test recite(1, 8) == expected
end

end
Loading