-
-
Notifications
You must be signed in to change notification settings - Fork 402
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"] | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.