From 146d1d3f8963f38ca4ca1cb85c9a753e7d815038 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Thu, 27 Jun 2024 17:22:40 -0400 Subject: [PATCH] raindrops --- elixir/raindrops/.exercism/config.json | 33 +++++++++++ elixir/raindrops/.exercism/metadata.json | 1 + elixir/raindrops/.formatter.exs | 4 ++ elixir/raindrops/.gitignore | 24 ++++++++ elixir/raindrops/HELP.md | 75 ++++++++++++++++++++++++ elixir/raindrops/README.md | 58 ++++++++++++++++++ elixir/raindrops/lib/raindrops.ex | 30 ++++++++++ elixir/raindrops/mix.exs | 28 +++++++++ elixir/raindrops/test/raindrops_test.exs | 71 ++++++++++++++++++++++ elixir/raindrops/test/test_helper.exs | 2 + 10 files changed, 326 insertions(+) create mode 100644 elixir/raindrops/.exercism/config.json create mode 100644 elixir/raindrops/.exercism/metadata.json create mode 100644 elixir/raindrops/.formatter.exs create mode 100644 elixir/raindrops/.gitignore create mode 100644 elixir/raindrops/HELP.md create mode 100644 elixir/raindrops/README.md create mode 100644 elixir/raindrops/lib/raindrops.ex create mode 100644 elixir/raindrops/mix.exs create mode 100644 elixir/raindrops/test/raindrops_test.exs create mode 100644 elixir/raindrops/test/test_helper.exs diff --git a/elixir/raindrops/.exercism/config.json b/elixir/raindrops/.exercism/config.json new file mode 100644 index 0000000..bfd5139 --- /dev/null +++ b/elixir/raindrops/.exercism/config.json @@ -0,0 +1,33 @@ +{ + "authors": [ + "rubysolo" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "jinyeow", + "lpil", + "neenjaw", + "parkerl", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/raindrops.ex" + ], + "test": [ + "test/raindrops_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.", + "source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.", + "source_url": "https://en.wikipedia.org/wiki/Fizz_buzz" +} diff --git a/elixir/raindrops/.exercism/metadata.json b/elixir/raindrops/.exercism/metadata.json new file mode 100644 index 0000000..1c6eb77 --- /dev/null +++ b/elixir/raindrops/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"raindrops","id":"e9ff118098084dc9befe7fe0a2e71ed1","url":"https://exercism.org/tracks/elixir/exercises/raindrops","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/raindrops/.formatter.exs b/elixir/raindrops/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/raindrops/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/raindrops/.gitignore b/elixir/raindrops/.gitignore new file mode 100644 index 0000000..5d93fac --- /dev/null +++ b/elixir/raindrops/.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"). +raindrops-*.tar + diff --git a/elixir/raindrops/HELP.md b/elixir/raindrops/HELP.md new file mode 100644 index 0000000..16e6cd6 --- /dev/null +++ b/elixir/raindrops/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/raindrops.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/raindrops/README.md b/elixir/raindrops/README.md new file mode 100644 index 0000000..5377277 --- /dev/null +++ b/elixir/raindrops/README.md @@ -0,0 +1,58 @@ +# Raindrops + +Welcome to Raindrops on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question. + +## Instructions + +Your task is to convert a number into its corresponding raindrop sounds. + +If a given number: + +- is divisible by 3, add "Pling" to the result. +- is divisible by 5, add "Plang" to the result. +- is divisible by 7, add "Plong" to the result. +- **is not** divisible by 3, 5, or 7, the result should be the number as a string. + +## Examples + +- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`. +- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`. +- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`. + +~~~~exercism/note +A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero. +Most languages provide operators or functions for one (or both) of these. + +[remainder]: https://exercism.org/docs/programming/operators/remainder +[modulo]: https://en.wikipedia.org/wiki/Modulo_operation +~~~~ + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @jinyeow +- @lpil +- @neenjaw +- @parkerl +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz \ No newline at end of file diff --git a/elixir/raindrops/lib/raindrops.ex b/elixir/raindrops/lib/raindrops.ex new file mode 100644 index 0000000..077c66c --- /dev/null +++ b/elixir/raindrops/lib/raindrops.ex @@ -0,0 +1,30 @@ +defmodule Raindrops do + @doc """ + Returns a string based on raindrop factors. + + - If the number contains 3 as a prime factor, output 'Pling'. + - If the number contains 5 as a prime factor, output 'Plang'. + - If the number contains 7 as a prime factor, output 'Plong'. + - If the number does not contain 3, 5, or 7 as a prime factor, + just pass the number's digits straight through. + """ + @results [ + {3, "Pling"}, + {5, "Plang"}, + {7, "Plong"} + ] + + @spec convert(pos_integer) :: String.t() + def convert(number) do + @results + |> Enum.map_join(&mapper(number, &1)) + |> case do + "" -> Integer.to_string(number) + result -> result + end + end + + defp mapper(number, {factor, value}) do + if rem(number, factor) == 0, do: value, else: "" + end +end diff --git a/elixir/raindrops/mix.exs b/elixir/raindrops/mix.exs new file mode 100644 index 0000000..bf45a26 --- /dev/null +++ b/elixir/raindrops/mix.exs @@ -0,0 +1,28 @@ +defmodule Raindrops.MixProject do + use Mix.Project + + def project do + [ + app: :raindrops, + 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/raindrops/test/raindrops_test.exs b/elixir/raindrops/test/raindrops_test.exs new file mode 100644 index 0000000..b68c64f --- /dev/null +++ b/elixir/raindrops/test/raindrops_test.exs @@ -0,0 +1,71 @@ +defmodule RaindropsTest do + use ExUnit.Case + + test "1" do + assert Raindrops.convert(1) == "1" + end + + test "3" do + assert Raindrops.convert(3) == "Pling" + end + + test "5" do + assert Raindrops.convert(5) == "Plang" + end + + test "7" do + assert Raindrops.convert(7) == "Plong" + end + + test "6" do + assert Raindrops.convert(6) == "Pling" + end + + test "8" do + assert Raindrops.convert(8) == "8" + end + + test "9" do + assert Raindrops.convert(9) == "Pling" + end + + test "10" do + assert Raindrops.convert(10) == "Plang" + end + + test "14" do + assert Raindrops.convert(14) == "Plong" + end + + test "15" do + assert Raindrops.convert(15) == "PlingPlang" + end + + test "21" do + assert Raindrops.convert(21) == "PlingPlong" + end + + test "25" do + assert Raindrops.convert(25) == "Plang" + end + + test "35" do + assert Raindrops.convert(35) == "PlangPlong" + end + + test "49" do + assert Raindrops.convert(49) == "Plong" + end + + test "52" do + assert Raindrops.convert(52) == "52" + end + + test "105" do + assert Raindrops.convert(105) == "PlingPlangPlong" + end + + test "12121" do + assert Raindrops.convert(12121) == "12121" + end +end diff --git a/elixir/raindrops/test/test_helper.exs b/elixir/raindrops/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/raindrops/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)