From c7b7989ccd8f3499c23e42573d0e58a48f7e8e21 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Thu, 27 Jun 2024 17:36:16 -0400 Subject: [PATCH] resistor-color-duo --- .../resistor-color-duo/.exercism/config.json | 19 +++++ .../.exercism/metadata.json | 1 + elixir/resistor-color-duo/.formatter.exs | 4 + elixir/resistor-color-duo/.gitignore | 24 ++++++ elixir/resistor-color-duo/HELP.md | 75 +++++++++++++++++++ elixir/resistor-color-duo/README.md | 48 ++++++++++++ .../lib/resistor_color_duo.ex | 25 +++++++ elixir/resistor-color-duo/mix.exs | 28 +++++++ .../test/resistor_color_duo_test.exs | 59 +++++++++++++++ .../resistor-color-duo/test/test_helper.exs | 2 + 10 files changed, 285 insertions(+) create mode 100644 elixir/resistor-color-duo/.exercism/config.json create mode 100644 elixir/resistor-color-duo/.exercism/metadata.json create mode 100644 elixir/resistor-color-duo/.formatter.exs create mode 100644 elixir/resistor-color-duo/.gitignore create mode 100644 elixir/resistor-color-duo/HELP.md create mode 100644 elixir/resistor-color-duo/README.md create mode 100644 elixir/resistor-color-duo/lib/resistor_color_duo.ex create mode 100644 elixir/resistor-color-duo/mix.exs create mode 100644 elixir/resistor-color-duo/test/resistor_color_duo_test.exs create mode 100644 elixir/resistor-color-duo/test/test_helper.exs diff --git a/elixir/resistor-color-duo/.exercism/config.json b/elixir/resistor-color-duo/.exercism/config.json new file mode 100644 index 0000000..b5e027e --- /dev/null +++ b/elixir/resistor-color-duo/.exercism/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jiegillet" + ], + "files": { + "solution": [ + "lib/resistor_color_duo.ex" + ], + "test": [ + "test/resistor_color_duo_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Convert color codes, as used on resistors, to a numeric value.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1464" +} diff --git a/elixir/resistor-color-duo/.exercism/metadata.json b/elixir/resistor-color-duo/.exercism/metadata.json new file mode 100644 index 0000000..9d90b27 --- /dev/null +++ b/elixir/resistor-color-duo/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"resistor-color-duo","id":"e609b31ae73f414fac5537059d7440c0","url":"https://exercism.org/tracks/elixir/exercises/resistor-color-duo","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/resistor-color-duo/.formatter.exs b/elixir/resistor-color-duo/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/resistor-color-duo/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/resistor-color-duo/.gitignore b/elixir/resistor-color-duo/.gitignore new file mode 100644 index 0000000..97ed986 --- /dev/null +++ b/elixir/resistor-color-duo/.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"). +resistor_color_duo-*.tar + diff --git a/elixir/resistor-color-duo/HELP.md b/elixir/resistor-color-duo/HELP.md new file mode 100644 index 0000000..f946e11 --- /dev/null +++ b/elixir/resistor-color-duo/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/resistor_color_duo.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/resistor-color-duo/README.md b/elixir/resistor-color-duo/README.md new file mode 100644 index 0000000..2a08888 --- /dev/null +++ b/elixir/resistor-color-duo/README.md @@ -0,0 +1,48 @@ +# Resistor Color Duo + +Welcome to Resistor Color Duo on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. +For this exercise, you need to know two things about them: + +- Each resistor has a resistance value. +- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. + +To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. +Each band has a position and a numeric value. + +The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. +For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15. + +In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. +The program will take color names as input and output a two digit number, even if the input is more than two colors! + +The band colors are encoded as follows: + +- Black: 0 +- Brown: 1 +- Red: 2 +- Orange: 3 +- Yellow: 4 +- Green: 5 +- Blue: 6 +- Violet: 7 +- Grey: 8 +- White: 9 + +From the example above: +brown-green should return 15 +brown-green-violet should return 15 too, ignoring the third color. + +## Source + +### Created by + +- @jiegillet + +### Based on + +Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1464 \ No newline at end of file diff --git a/elixir/resistor-color-duo/lib/resistor_color_duo.ex b/elixir/resistor-color-duo/lib/resistor_color_duo.ex new file mode 100644 index 0000000..583d007 --- /dev/null +++ b/elixir/resistor-color-duo/lib/resistor_color_duo.ex @@ -0,0 +1,25 @@ +defmodule ResistorColorDuo do + @doc """ + Calculate a resistance value from two colors + """ + @spec value(colors :: [atom]) :: integer + def value(colors) do + colors + |> Enum.take(2) + |> Enum.map(&code/1) + |> Integer.undigits() + end + + + @spec code(atom) :: integer() + defp code(:black), do: 0 + defp code(:brown), do: 1 + defp code(:red), do: 2 + defp code(:orange), do: 3 + defp code(:yellow), do: 4 + defp code(:green), do: 5 + defp code(:blue), do: 6 + defp code(:violet), do: 7 + defp code(:grey), do: 8 + defp code(:white), do: 9 +end diff --git a/elixir/resistor-color-duo/mix.exs b/elixir/resistor-color-duo/mix.exs new file mode 100644 index 0000000..0c2f815 --- /dev/null +++ b/elixir/resistor-color-duo/mix.exs @@ -0,0 +1,28 @@ +defmodule ResistorColorDuo.MixProject do + use Mix.Project + + def project do + [ + app: :resistor_color_duo, + 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/resistor-color-duo/test/resistor_color_duo_test.exs b/elixir/resistor-color-duo/test/resistor_color_duo_test.exs new file mode 100644 index 0000000..51d4ef9 --- /dev/null +++ b/elixir/resistor-color-duo/test/resistor_color_duo_test.exs @@ -0,0 +1,59 @@ +defmodule ResistorColorDuoTest do + use ExUnit.Case + + test "Brown and black" do + colors = [:brown, :black] + output = ResistorColorDuo.value(colors) + expected = 10 + + assert output == expected + end + + test "Blue and grey" do + colors = [:blue, :grey] + output = ResistorColorDuo.value(colors) + expected = 68 + + assert output == expected + end + + test "Yellow and violet" do + colors = [:yellow, :violet] + output = ResistorColorDuo.value(colors) + expected = 47 + + assert output == expected + end + + test "White and red" do + colors = [:white, :red] + output = ResistorColorDuo.value(colors) + expected = 92 + + assert output == expected + end + + test "Orange and orange" do + colors = [:orange, :orange] + output = ResistorColorDuo.value(colors) + expected = 33 + + assert output == expected + end + + test "Ignore additional colors" do + colors = [:green, :brown, :orange] + output = ResistorColorDuo.value(colors) + expected = 51 + + assert output == expected + end + + test "Black and brown, one digit" do + colors = [:black, :brown] + output = ResistorColorDuo.value(colors) + expected = 1 + + assert output == expected + end +end diff --git a/elixir/resistor-color-duo/test/test_helper.exs b/elixir/resistor-color-duo/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/resistor-color-duo/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)