house
This commit is contained in:
parent
2eb14fcd40
commit
7b3e6d3216
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"jiegillet"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"angelikatyborska"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"lib/house.ex"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/house_test.exs"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.ex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Output the nursery rhyme 'This is the House that Jack Built'.",
|
||||||
|
"source": "British nursery rhyme",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built"
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"house","id":"f8fb50f62b7e4a238d66e449ea696bd7","url":"https://exercism.org/tracks/elixir/exercises/house","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -0,0 +1,24 @@
|
||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
house-*.tar
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
From the terminal, change to the base directory of the exercise then execute the tests with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test
|
||||||
|
```
|
||||||
|
|
||||||
|
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
|
||||||
|
|
||||||
|
Documentation:
|
||||||
|
|
||||||
|
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
|
||||||
|
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
|
||||||
|
|
||||||
|
## Pending tests
|
||||||
|
|
||||||
|
In test suites of practice exercises, all but the first test have been tagged to be skipped.
|
||||||
|
|
||||||
|
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# @tag :pending
|
||||||
|
test "shouting" do
|
||||||
|
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ mix test --include pending
|
||||||
|
```
|
||||||
|
|
||||||
|
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
# ExUnit.configure(exclude: :pending, trace: true)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Useful `mix test` options
|
||||||
|
|
||||||
|
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
|
||||||
|
* `--failed` - runs only tests that failed the last time they ran
|
||||||
|
* `--max-failures` - the suite stops evaluating tests when this number of test failures
|
||||||
|
is reached
|
||||||
|
* `--seed 0` - disables randomization so the tests in a single file will always be ran
|
||||||
|
in the same order they were defined in
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit lib/house.ex` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
|
||||||
|
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.
|
|
@ -0,0 +1,124 @@
|
||||||
|
# House
|
||||||
|
|
||||||
|
Welcome to House on Exercism's Elixir Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Recite the nursery rhyme 'This is the House that Jack Built'.
|
||||||
|
|
||||||
|
> [The] process of placing a phrase of clause within another phrase of clause is called embedding.
|
||||||
|
> It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions.
|
||||||
|
> Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.
|
||||||
|
|
||||||
|
- [papyr.com][papyr]
|
||||||
|
|
||||||
|
The nursery rhyme reads as follows:
|
||||||
|
|
||||||
|
```text
|
||||||
|
This is the house that Jack built.
|
||||||
|
|
||||||
|
This is the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the man all tattered and torn
|
||||||
|
that kissed the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the priest all shaven and shorn
|
||||||
|
that married the man all tattered and torn
|
||||||
|
that kissed the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the rooster that crowed in the morn
|
||||||
|
that woke the priest all shaven and shorn
|
||||||
|
that married the man all tattered and torn
|
||||||
|
that kissed the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the farmer sowing his corn
|
||||||
|
that kept the rooster that crowed in the morn
|
||||||
|
that woke the priest all shaven and shorn
|
||||||
|
that married the man all tattered and torn
|
||||||
|
that kissed the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
|
||||||
|
This is the horse and the hound and the horn
|
||||||
|
that belonged to the farmer sowing his corn
|
||||||
|
that kept the rooster that crowed in the morn
|
||||||
|
that woke the priest all shaven and shorn
|
||||||
|
that married the man all tattered and torn
|
||||||
|
that kissed the maiden all forlorn
|
||||||
|
that milked the cow with the crumpled horn
|
||||||
|
that tossed the dog
|
||||||
|
that worried the cat
|
||||||
|
that killed the rat
|
||||||
|
that ate the malt
|
||||||
|
that lay in the house that Jack built.
|
||||||
|
```
|
||||||
|
|
||||||
|
[papyr]: https://papyr.com/hypertextbooks/grammar/ph_noun.htm
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @jiegillet
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @angelikatyborska
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
British nursery rhyme - https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built
|
|
@ -0,0 +1,36 @@
|
||||||
|
defmodule House do
|
||||||
|
@doc """
|
||||||
|
Return verses of the nursery rhyme 'This is the House that Jack Built'.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@this_is "This is"
|
||||||
|
@verses [
|
||||||
|
"house that Jack built",
|
||||||
|
"malt that lay in",
|
||||||
|
"rat that ate",
|
||||||
|
"cat that killed",
|
||||||
|
"dog that worried",
|
||||||
|
"cow with the crumpled horn that tossed",
|
||||||
|
"maiden all forlorn that milked",
|
||||||
|
"man all tattered and torn that kissed",
|
||||||
|
"priest all shaven and shorn that married",
|
||||||
|
"rooster that crowed in the morn that woke",
|
||||||
|
"farmer sowing his corn that kept",
|
||||||
|
"horse and the hound and the horn that belonged to"
|
||||||
|
]
|
||||||
|
|
||||||
|
@spec recite(start :: integer, stop :: integer) :: String.t()
|
||||||
|
def recite(start, stop) do
|
||||||
|
(Enum.map(start..stop, &verse/1) |> Enum.join("\n")) <> "\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp verse(i) do
|
||||||
|
verse =
|
||||||
|
@verses
|
||||||
|
|> Enum.take(i)
|
||||||
|
|> Enum.reverse()
|
||||||
|
|> Enum.join(" the ")
|
||||||
|
|
||||||
|
@this_is <> " the " <> verse <> "."
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule House.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :house,
|
||||||
|
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
|
|
@ -0,0 +1,186 @@
|
||||||
|
defmodule HouseTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "verse one - the house that jack built" do
|
||||||
|
start = 1
|
||||||
|
stop = 1
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse two - the malt that lay" do
|
||||||
|
start = 2
|
||||||
|
stop = 2
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse three - the rat that ate" do
|
||||||
|
start = 3
|
||||||
|
stop = 3
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse four - the cat that killed" do
|
||||||
|
start = 4
|
||||||
|
stop = 4
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse five - the dog that worried" do
|
||||||
|
start = 5
|
||||||
|
stop = 5
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse six - the cow with the crumpled horn" do
|
||||||
|
start = 6
|
||||||
|
stop = 6
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse seven - the maiden all forlorn" do
|
||||||
|
start = 7
|
||||||
|
stop = 7
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse eight - the man all tattered and torn" do
|
||||||
|
start = 8
|
||||||
|
stop = 8
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse nine - the priest all shaven and shorn" do
|
||||||
|
start = 9
|
||||||
|
stop = 9
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse 10 - the rooster that crowed in the morn" do
|
||||||
|
start = 10
|
||||||
|
stop = 10
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse 11 - the farmer sowing his corn" do
|
||||||
|
start = 11
|
||||||
|
stop = 11
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "verse 12 - the horse and the hound and the horn" do
|
||||||
|
start = 12
|
||||||
|
stop = 12
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "multiple verses" do
|
||||||
|
start = 4
|
||||||
|
stop = 8
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
|
||||||
|
test "full rhyme" do
|
||||||
|
start = 1
|
||||||
|
stop = 12
|
||||||
|
output = House.recite(start, stop)
|
||||||
|
|
||||||
|
expected = """
|
||||||
|
This is the house that Jack built.
|
||||||
|
This is the malt that lay in the house that Jack built.
|
||||||
|
This is the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert output == expected
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue