Skip to content

New practice exercise food-chain #780

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

Merged
merged 3 commits into from
Jun 28, 2021
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
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,22 @@
],
"difficulty": 2
},
{
"slug": "food-chain",
"name": "Food Chain",
"uuid": "7541eeac-169f-4b28-b9d2-7125225b121b",
"prerequisites": [
"lists",
"strings",
"pattern-matching",
"multiple-clause-functions",
"enum"
],
"practices": [
"strings"
],
"difficulty": 5
},
{
"slug": "affine-cipher",
"name": "Affine Cipher",
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 @@
# Description

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](http://en.wikipedia.org/wiki/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!
```
4 changes: 4 additions & 0 deletions exercises/practice/food-chain/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
18 changes: 18 additions & 0 deletions exercises/practice/food-chain/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": ["jiegillet"],
"contributors": [],
"files": {
"example": [
".meta/example.ex"
],
"solution": [
"lib/food_chain.ex"
],
"test": [
"test/food_chain_test.exs"
]
},
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
"source": "Wikipedia",
"source_url": "http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
}
54 changes: 54 additions & 0 deletions exercises/practice/food-chain/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule FoodChain do
@animals [
fly: "I don't know why she swallowed the fly.",
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!"
]

@doc """
Generate consecutive verses of the song 'I Know an Old Lady Who Swallowed a Fly'.
"""
@spec recite(start :: integer, stop :: integer) :: String.t()
def recite(start, stop), do: Enum.map_join(start..stop, "\n", &verse/1)

def verse(1),
do: """
I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.
"""

def verse(8),
do: """
I know an old lady who swallowed a horse.
She's dead, of course!
"""

def verse(number) do
[{animal, line} | _] =
animals =
Enum.take(@animals, number)
|> Enum.reverse()

cumulative_lines =
Enum.zip(animals, tl(animals))
|> Enum.map_join("\n", fn
{{:bird, _}, _} ->
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."

{{a, _}, {b, _}} ->
"She swallowed the #{a} to catch the #{b}."
end)

"""
I know an old lady who swallowed a #{animal}.
#{line}
#{cumulative_lines}
I don't know why she swallowed the fly. Perhaps she'll die.
"""
end
end
39 changes: 39 additions & 0 deletions exercises/practice/food-chain/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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"
8 changes: 8 additions & 0 deletions exercises/practice/food-chain/lib/food_chain.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule FoodChain do
@doc """
Generate consecutive verses of the song 'I Know an Old Lady Who Swallowed a Fly'.
"""
@spec recite(start :: integer, stop :: integer) :: String.t()
def recite(start, stop) do
end
end
28 changes: 28 additions & 0 deletions exercises/practice/food-chain/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule FoodChain.MixProject do
use Mix.Project

def project do
[
app: :food_chain,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
Loading