From 7856986097bf57e92d47a83449b6a45c0e684ae5 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 21 Aug 2024 11:11:30 -0400 Subject: [PATCH] proverb --- elixir/proverb/.exercism/config.json | 22 ++++++++ elixir/proverb/.exercism/metadata.json | 1 + elixir/proverb/.formatter.exs | 4 ++ elixir/proverb/.gitignore | 24 ++++++++ elixir/proverb/HELP.md | 75 +++++++++++++++++++++++++ elixir/proverb/README.md | 38 +++++++++++++ elixir/proverb/lib/proverb.ex | 17 ++++++ elixir/proverb/mix.exs | 28 +++++++++ elixir/proverb/test/proverb_test.exs | 78 ++++++++++++++++++++++++++ elixir/proverb/test/test_helper.exs | 2 + 10 files changed, 289 insertions(+) create mode 100644 elixir/proverb/.exercism/config.json create mode 100644 elixir/proverb/.exercism/metadata.json create mode 100644 elixir/proverb/.formatter.exs create mode 100644 elixir/proverb/.gitignore create mode 100644 elixir/proverb/HELP.md create mode 100644 elixir/proverb/README.md create mode 100644 elixir/proverb/lib/proverb.ex create mode 100644 elixir/proverb/mix.exs create mode 100644 elixir/proverb/test/proverb_test.exs create mode 100644 elixir/proverb/test/test_helper.exs diff --git a/elixir/proverb/.exercism/config.json b/elixir/proverb/.exercism/config.json new file mode 100644 index 0000000..0501c13 --- /dev/null +++ b/elixir/proverb/.exercism/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "jiegillet" + ], + "contributors": [ + "angelikatyborska" + ], + "files": { + "solution": [ + "lib/proverb.ex" + ], + "test": [ + "test/proverb_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail" +} diff --git a/elixir/proverb/.exercism/metadata.json b/elixir/proverb/.exercism/metadata.json new file mode 100644 index 0000000..d00774e --- /dev/null +++ b/elixir/proverb/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"proverb","id":"ea1c76c464144259a45e86a6fa8b920b","url":"https://exercism.org/tracks/elixir/exercises/proverb","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/proverb/.formatter.exs b/elixir/proverb/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/proverb/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/proverb/.gitignore b/elixir/proverb/.gitignore new file mode 100644 index 0000000..eb2b6de --- /dev/null +++ b/elixir/proverb/.gitignore @@ -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"). +proverb-*.tar + diff --git a/elixir/proverb/HELP.md b/elixir/proverb/HELP.md new file mode 100644 index 0000000..9c5d157 --- /dev/null +++ b/elixir/proverb/HELP.md @@ -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/.exs:LINENUM` - runs only a single test, the test from `.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/proverb.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. \ No newline at end of file diff --git a/elixir/proverb/README.md b/elixir/proverb/README.md new file mode 100644 index 0000000..ebbeabe --- /dev/null +++ b/elixir/proverb/README.md @@ -0,0 +1,38 @@ +# Proverb + +Welcome to Proverb on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +For want of a horseshoe nail, a kingdom was lost, or so the saying goes. + +Given a list of inputs, generate the relevant proverb. +For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme: + +```text +For want of a nail the shoe was lost. +For want of a shoe the horse was lost. +For want of a horse the rider was lost. +For want of a rider the message was lost. +For want of a message the battle was lost. +For want of a battle the kingdom was lost. +And all for the want of a nail. +``` + +Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. +No line of the output text should be a static, unchanging string; all should vary according to the input given. + +## Source + +### Created by + +- @jiegillet + +### Contributed to by + +- @angelikatyborska + +### Based on + +Wikipedia - https://en.wikipedia.org/wiki/For_Want_of_a_Nail \ No newline at end of file diff --git a/elixir/proverb/lib/proverb.ex b/elixir/proverb/lib/proverb.ex new file mode 100644 index 0000000..29d24fc --- /dev/null +++ b/elixir/proverb/lib/proverb.ex @@ -0,0 +1,17 @@ +defmodule Proverb do + @doc """ + Generate a proverb from a list of strings. + """ + @spec recite(strings :: [String.t()]) :: String.t() + def recite([]), do: "" + def recite([one]), do: first_in_the_end(one) + def recite([head | tail]), do: Enum.join([first(head), recite_proverb(tail), first_in_the_end(head)]) + + defp recite_proverb([last]), do: last(last) + defp recite_proverb([middle | tail]), do: middle(middle) <> " " <> recite_proverb(tail) + + defp first(first), do: "For want of a #{first} " + defp middle(middle), do: "the #{middle} was lost.\nFor want of a #{middle}" + defp last(last), do: "the #{last} was lost.\n" + defp first_in_the_end(first), do: "And all for the want of a #{first}.\n" +end diff --git a/elixir/proverb/mix.exs b/elixir/proverb/mix.exs new file mode 100644 index 0000000..54231a9 --- /dev/null +++ b/elixir/proverb/mix.exs @@ -0,0 +1,28 @@ +defmodule Proverb.MixProject do + use Mix.Project + + def project do + [ + app: :proverb, + 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 diff --git a/elixir/proverb/test/proverb_test.exs b/elixir/proverb/test/proverb_test.exs new file mode 100644 index 0000000..ddb81c3 --- /dev/null +++ b/elixir/proverb/test/proverb_test.exs @@ -0,0 +1,78 @@ +defmodule ProverbTest do + use ExUnit.Case + + test "zero pieces" do + strings = [] + output = Proverb.recite(strings) + expected = "" + + assert output == expected + end + + test "one piece" do + strings = ["nail"] + output = Proverb.recite(strings) + + expected = """ + And all for the want of a nail. + """ + + assert output == expected + end + + test "two pieces" do + strings = ["nail", "shoe"] + output = Proverb.recite(strings) + + expected = """ + For want of a nail the shoe was lost. + And all for the want of a nail. + """ + + assert output == expected + end + + test "three pieces" do + strings = ["nail", "shoe", "horse"] + output = Proverb.recite(strings) + + expected = """ + For want of a nail the shoe was lost. + For want of a shoe the horse was lost. + And all for the want of a nail. + """ + + assert output == expected + end + + test "full proverb" do + strings = ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"] + output = Proverb.recite(strings) + + expected = """ + For want of a nail the shoe was lost. + For want of a shoe the horse was lost. + For want of a horse the rider was lost. + For want of a rider the message was lost. + For want of a message the battle was lost. + For want of a battle the kingdom was lost. + And all for the want of a nail. + """ + + assert output == expected + end + + test "four pieces modernized" do + strings = ["pin", "gun", "soldier", "battle"] + output = Proverb.recite(strings) + + expected = """ + For want of a pin the gun was lost. + For want of a gun the soldier was lost. + For want of a soldier the battle was lost. + And all for the want of a pin. + """ + + assert output == expected + end +end diff --git a/elixir/proverb/test/test_helper.exs b/elixir/proverb/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/proverb/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)