From 0aef8da6d9ceaac187f3b9cbd2d489ab71c326a6 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 1 Jul 2024 14:35:51 -0400 Subject: [PATCH] bottle_song --- elixir/bottle-song/.exercism/config.json | 19 +++ elixir/bottle-song/.exercism/metadata.json | 1 + elixir/bottle-song/.formatter.exs | 4 + elixir/bottle-song/.gitignore | 26 ++++ elixir/bottle-song/HELP.md | 75 ++++++++++ elixir/bottle-song/README.md | 72 ++++++++++ elixir/bottle-song/lib/bottle_song.ex | 28 ++++ elixir/bottle-song/mix.exs | 28 ++++ elixir/bottle-song/test/bottle_song_test.exs | 137 +++++++++++++++++++ elixir/bottle-song/test/test_helper.exs | 2 + 10 files changed, 392 insertions(+) create mode 100644 elixir/bottle-song/.exercism/config.json create mode 100644 elixir/bottle-song/.exercism/metadata.json create mode 100644 elixir/bottle-song/.formatter.exs create mode 100644 elixir/bottle-song/.gitignore create mode 100644 elixir/bottle-song/HELP.md create mode 100644 elixir/bottle-song/README.md create mode 100644 elixir/bottle-song/lib/bottle_song.ex create mode 100644 elixir/bottle-song/mix.exs create mode 100644 elixir/bottle-song/test/bottle_song_test.exs create mode 100644 elixir/bottle-song/test/test_helper.exs diff --git a/elixir/bottle-song/.exercism/config.json b/elixir/bottle-song/.exercism/config.json new file mode 100644 index 0000000..a10e829 --- /dev/null +++ b/elixir/bottle-song/.exercism/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "antoine-duchenet" + ], + "files": { + "solution": [ + "lib/bottle_song.ex" + ], + "test": [ + "test/bottle_song_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Produce the lyrics to the popular children's repetitive song: Ten Green Bottles.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Ten_Green_Bottles" +} diff --git a/elixir/bottle-song/.exercism/metadata.json b/elixir/bottle-song/.exercism/metadata.json new file mode 100644 index 0000000..1f8cdfa --- /dev/null +++ b/elixir/bottle-song/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"bottle-song","id":"0826bdf779e94ea6bc38fc08189a0e97","url":"https://exercism.org/tracks/elixir/exercises/bottle-song","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/bottle-song/.formatter.exs b/elixir/bottle-song/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/bottle-song/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/bottle-song/.gitignore b/elixir/bottle-song/.gitignore new file mode 100644 index 0000000..9801513 --- /dev/null +++ b/elixir/bottle-song/.gitignore @@ -0,0 +1,26 @@ +# 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"). +bottle_song-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/elixir/bottle-song/HELP.md b/elixir/bottle-song/HELP.md new file mode 100644 index 0000000..14f4044 --- /dev/null +++ b/elixir/bottle-song/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/bottle_song.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/bottle-song/README.md b/elixir/bottle-song/README.md new file mode 100644 index 0000000..8793662 --- /dev/null +++ b/elixir/bottle-song/README.md @@ -0,0 +1,72 @@ +# Bottle Song + +Welcome to Bottle Song on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Recite the lyrics to that popular children's repetitive song: Ten Green Bottles. + +Note that not all verses are identical. + +```text +Ten green bottles hanging on the wall, +Ten green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be nine green bottles hanging on the wall. + +Nine green bottles hanging on the wall, +Nine green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be eight green bottles hanging on the wall. + +Eight green bottles hanging on the wall, +Eight green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be seven green bottles hanging on the wall. + +Seven green bottles hanging on the wall, +Seven green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be six green bottles hanging on the wall. + +Six green bottles hanging on the wall, +Six green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be five green bottles hanging on the wall. + +Five green bottles hanging on the wall, +Five green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be four green bottles hanging on the wall. + +Four green bottles hanging on the wall, +Four green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be three green bottles hanging on the wall. + +Three green bottles hanging on the wall, +Three green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be two green bottles hanging on the wall. + +Two green bottles hanging on the wall, +Two green bottles hanging on the wall, +And if one green bottle should accidentally fall, +There'll be one green bottle hanging on the wall. + +One green bottle hanging on the wall, +One green bottle hanging on the wall, +And if one green bottle should accidentally fall, +There'll be no green bottles hanging on the wall. +``` + +## Source + +### Created by + +- @antoine-duchenet + +### Based on + +Wikipedia - https://en.wikipedia.org/wiki/Ten_Green_Bottles \ No newline at end of file diff --git a/elixir/bottle-song/lib/bottle_song.ex b/elixir/bottle-song/lib/bottle_song.ex new file mode 100644 index 0000000..a7004df --- /dev/null +++ b/elixir/bottle-song/lib/bottle_song.ex @@ -0,0 +1,28 @@ +defmodule BottleSong do + @moduledoc """ + Handles lyrics of the popular children song: Ten Green Bottles + """ + + defp verse(n) do + bottle_sense = fn + 0 -> "No green bottles" + 1 -> "One green bottle" + 2 -> "Two green bottles" + 3 -> "Three green bottles" + 4 -> "Four green bottles" + 5 -> "Five green bottles" + 6 -> "Six green bottles" + 7 -> "Seven green bottles" + 8 -> "Eight green bottles" + 9 -> "Nine green bottles" + 10 -> "Ten green bottles" + end + + "#{bottle_sense.(n)} hanging on the wall,\n#{bottle_sense.(n)} hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be #{String.downcase(bottle_sense.(n-1))} hanging on the wall." + end + + @spec recite(pos_integer, pos_integer) :: String.t() + def recite(start_bottle, take_down) do + Enum.map_join(0..(take_down - 1), "\n\n", &verse(start_bottle - &1)) + end +end diff --git a/elixir/bottle-song/mix.exs b/elixir/bottle-song/mix.exs new file mode 100644 index 0000000..b6cac60 --- /dev/null +++ b/elixir/bottle-song/mix.exs @@ -0,0 +1,28 @@ +defmodule BottleSong.MixProject do + use Mix.Project + + def project do + [ + app: :bottle_song, + version: "1.0.0", + elixir: "~> 1.10", + 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/bottle-song/test/bottle_song_test.exs b/elixir/bottle-song/test/bottle_song_test.exs new file mode 100644 index 0000000..1d5ba89 --- /dev/null +++ b/elixir/bottle-song/test/bottle_song_test.exs @@ -0,0 +1,137 @@ +defmodule BottleSongTest do + use ExUnit.Case + + describe "[Single verse]" do + test "First generic verse" do + assert BottleSong.recite(10, 1) == + """ + Ten green bottles hanging on the wall, + Ten green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be nine green bottles hanging on the wall.\ + """ + end + + test "Last generic verse" do + assert BottleSong.recite(3, 1) == + """ + Three green bottles hanging on the wall, + Three green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be two green bottles hanging on the wall.\ + """ + end + + test "Verse with 2 bottles" do + assert BottleSong.recite(2, 1) == + """ + Two green bottles hanging on the wall, + Two green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be one green bottle hanging on the wall.\ + """ + end + + test "Verse with 1 bottle" do + assert BottleSong.recite(1, 1) == + """ + One green bottle hanging on the wall, + One green bottle hanging on the wall, + And if one green bottle should accidentally fall, + There'll be no green bottles hanging on the wall.\ + """ + end + end + + describe "[Multiple verses]" do + test "First two verses" do + assert BottleSong.recite(10, 2) == + """ + Ten green bottles hanging on the wall, + Ten green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be nine green bottles hanging on the wall. + + Nine green bottles hanging on the wall, + Nine green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be eight green bottles hanging on the wall.\ + """ + end + + test "Last three verses" do + assert BottleSong.recite(3, 3) == + """ + Three green bottles hanging on the wall, + Three green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be two green bottles hanging on the wall. + + Two green bottles hanging on the wall, + Two green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be one green bottle hanging on the wall. + + One green bottle hanging on the wall, + One green bottle hanging on the wall, + And if one green bottle should accidentally fall, + There'll be no green bottles hanging on the wall.\ + """ + end + + test "All verses" do + assert BottleSong.recite(10, 10) == + """ + Ten green bottles hanging on the wall, + Ten green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be nine green bottles hanging on the wall. + + Nine green bottles hanging on the wall, + Nine green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be eight green bottles hanging on the wall. + + Eight green bottles hanging on the wall, + Eight green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be seven green bottles hanging on the wall. + + Seven green bottles hanging on the wall, + Seven green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be six green bottles hanging on the wall. + + Six green bottles hanging on the wall, + Six green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be five green bottles hanging on the wall. + + Five green bottles hanging on the wall, + Five green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be four green bottles hanging on the wall. + + Four green bottles hanging on the wall, + Four green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be three green bottles hanging on the wall. + + Three green bottles hanging on the wall, + Three green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be two green bottles hanging on the wall. + + Two green bottles hanging on the wall, + Two green bottles hanging on the wall, + And if one green bottle should accidentally fall, + There'll be one green bottle hanging on the wall. + + One green bottle hanging on the wall, + One green bottle hanging on the wall, + And if one green bottle should accidentally fall, + There'll be no green bottles hanging on the wall.\ + """ + end + end +end diff --git a/elixir/bottle-song/test/test_helper.exs b/elixir/bottle-song/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/bottle-song/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)