Skip to content

Commit 7eedb7b

Browse files
New practice exercise food-chain (#780)
Co-authored-by: Angelika Tyborska <[email protected]>
1 parent 967530f commit 7eedb7b

File tree

10 files changed

+462
-0
lines changed

10 files changed

+462
-0
lines changed

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,22 @@
25032503
],
25042504
"difficulty": 2
25052505
},
2506+
{
2507+
"slug": "food-chain",
2508+
"name": "Food Chain",
2509+
"uuid": "7541eeac-169f-4b28-b9d2-7125225b121b",
2510+
"prerequisites": [
2511+
"lists",
2512+
"strings",
2513+
"pattern-matching",
2514+
"multiple-clause-functions",
2515+
"enum"
2516+
],
2517+
"practices": [
2518+
"strings"
2519+
],
2520+
"difficulty": 5
2521+
},
25062522
{
25072523
"slug": "affine-cipher",
25082524
"name": "Affine Cipher",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Description
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,
6+
or read them from a file, this problem is much more
7+
interesting if you approach it algorithmically.
8+
9+
This is a [cumulative song](http://en.wikipedia.org/wiki/Cumulative_song) of unknown origin.
10+
11+
This is one of many common variants.
12+
13+
```text
14+
I know an old lady who swallowed a fly.
15+
I don't know why she swallowed the fly. Perhaps she'll die.
16+
17+
I know an old lady who swallowed a spider.
18+
It wriggled and jiggled and tickled inside her.
19+
She swallowed the spider to catch the fly.
20+
I don't know why she swallowed the fly. Perhaps she'll die.
21+
22+
I know an old lady who swallowed a bird.
23+
How absurd to swallow a bird!
24+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
25+
She swallowed the spider to catch the fly.
26+
I don't know why she swallowed the fly. Perhaps she'll die.
27+
28+
I know an old lady who swallowed a cat.
29+
Imagine that, to swallow a cat!
30+
She swallowed the cat to catch the bird.
31+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
32+
She swallowed the spider to catch the fly.
33+
I don't know why she swallowed the fly. Perhaps she'll die.
34+
35+
I know an old lady who swallowed a dog.
36+
What a hog, to swallow a dog!
37+
She swallowed the dog to catch the cat.
38+
She swallowed the cat to catch the bird.
39+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
40+
She swallowed the spider to catch the fly.
41+
I don't know why she swallowed the fly. Perhaps she'll die.
42+
43+
I know an old lady who swallowed a goat.
44+
Just opened her throat and swallowed a goat!
45+
She swallowed the goat to catch the dog.
46+
She swallowed the dog to catch the cat.
47+
She swallowed the cat to catch the bird.
48+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
49+
She swallowed the spider to catch the fly.
50+
I don't know why she swallowed the fly. Perhaps she'll die.
51+
52+
I know an old lady who swallowed a cow.
53+
I don't know how she swallowed a cow!
54+
She swallowed the cow to catch the goat.
55+
She swallowed the goat to catch the dog.
56+
She swallowed the dog to catch the cat.
57+
She swallowed the cat to catch the bird.
58+
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
59+
She swallowed the spider to catch the fly.
60+
I don't know why she swallowed the fly. Perhaps she'll die.
61+
62+
I know an old lady who swallowed a horse.
63+
She's dead, of course!
64+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": ["jiegillet"],
3+
"contributors": [],
4+
"files": {
5+
"example": [
6+
".meta/example.ex"
7+
],
8+
"solution": [
9+
"lib/food_chain.ex"
10+
],
11+
"test": [
12+
"test/food_chain_test.exs"
13+
]
14+
},
15+
"blurb": "Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.",
16+
"source": "Wikipedia",
17+
"source_url": "http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly"
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defmodule FoodChain do
2+
@animals [
3+
fly: "I don't know why she swallowed the fly.",
4+
spider: "It wriggled and jiggled and tickled inside her.",
5+
bird: "How absurd to swallow a bird!",
6+
cat: "Imagine that, to swallow a cat!",
7+
dog: "What a hog, to swallow a dog!",
8+
goat: "Just opened her throat and swallowed a goat!",
9+
cow: "I don't know how she swallowed a cow!",
10+
horse: "She's dead, of course!"
11+
]
12+
13+
@doc """
14+
Generate consecutive verses of the song 'I Know an Old Lady Who Swallowed a Fly'.
15+
"""
16+
@spec recite(start :: integer, stop :: integer) :: String.t()
17+
def recite(start, stop), do: Enum.map_join(start..stop, "\n", &verse/1)
18+
19+
def verse(1),
20+
do: """
21+
I know an old lady who swallowed a fly.
22+
I don't know why she swallowed the fly. Perhaps she'll die.
23+
"""
24+
25+
def verse(8),
26+
do: """
27+
I know an old lady who swallowed a horse.
28+
She's dead, of course!
29+
"""
30+
31+
def verse(number) do
32+
[{animal, line} | _] =
33+
animals =
34+
Enum.take(@animals, number)
35+
|> Enum.reverse()
36+
37+
cumulative_lines =
38+
Enum.zip(animals, tl(animals))
39+
|> Enum.map_join("\n", fn
40+
{{:bird, _}, _} ->
41+
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her."
42+
43+
{{a, _}, {b, _}} ->
44+
"She swallowed the #{a} to catch the #{b}."
45+
end)
46+
47+
"""
48+
I know an old lady who swallowed a #{animal}.
49+
#{line}
50+
#{cumulative_lines}
51+
I don't know why she swallowed the fly. Perhaps she'll die.
52+
"""
53+
end
54+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
[751dce68-9412-496e-b6e8-855998c56166]
12+
description = "fly"
13+
14+
[6c56f861-0c5e-4907-9a9d-b2efae389379]
15+
description = "spider"
16+
17+
[3edf5f33-bef1-4e39-ae67-ca5eb79203fa]
18+
description = "bird"
19+
20+
[e866a758-e1ff-400e-9f35-f27f28cc288f]
21+
description = "cat"
22+
23+
[3f02c30e-496b-4b2a-8491-bc7e2953cafb]
24+
description = "dog"
25+
26+
[4b3fd221-01ea-46e0-825b-5734634fbc59]
27+
description = "goat"
28+
29+
[1b707da9-7001-4fac-941f-22ad9c7a65d4]
30+
description = "cow"
31+
32+
[3cb10d46-ae4e-4d2c-9296-83c9ffc04cdc]
33+
description = "horse"
34+
35+
[22b863d5-17e4-4d1e-93e4-617329a5c050]
36+
description = "multiple verses"
37+
38+
[e626b32b-745c-4101-bcbd-3b13456893db]
39+
description = "full song"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule FoodChain do
2+
@doc """
3+
Generate consecutive verses of the song 'I Know an Old Lady Who Swallowed a Fly'.
4+
"""
5+
@spec recite(start :: integer, stop :: integer) :: String.t()
6+
def recite(start, stop) do
7+
end
8+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule FoodChain.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :food_chain,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

0 commit comments

Comments
 (0)