From 4265743d75bacf15bbd2bbffab25087f85fdf9bb Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 11 Mar 2024 11:20:18 -0400 Subject: [PATCH] resistor_color --- elixir/resistor-color/.exercism/config.json | 22 ++++++ elixir/resistor-color/.exercism/metadata.json | 1 + elixir/resistor-color/.formatter.exs | 4 + elixir/resistor-color/.gitignore | 24 ++++++ elixir/resistor-color/HELP.md | 75 +++++++++++++++++++ elixir/resistor-color/README.md | 60 +++++++++++++++ elixir/resistor-color/lib/resistor_color.ex | 20 +++++ elixir/resistor-color/mix.exs | 28 +++++++ .../test/resistor_color_test.exs | 53 +++++++++++++ elixir/resistor-color/test/test_helper.exs | 2 + 10 files changed, 289 insertions(+) create mode 100644 elixir/resistor-color/.exercism/config.json create mode 100644 elixir/resistor-color/.exercism/metadata.json create mode 100644 elixir/resistor-color/.formatter.exs create mode 100644 elixir/resistor-color/.gitignore create mode 100644 elixir/resistor-color/HELP.md create mode 100644 elixir/resistor-color/README.md create mode 100644 elixir/resistor-color/lib/resistor_color.ex create mode 100644 elixir/resistor-color/mix.exs create mode 100644 elixir/resistor-color/test/resistor_color_test.exs create mode 100644 elixir/resistor-color/test/test_helper.exs diff --git a/elixir/resistor-color/.exercism/config.json b/elixir/resistor-color/.exercism/config.json new file mode 100644 index 0000000..5185b3d --- /dev/null +++ b/elixir/resistor-color/.exercism/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "Bscruz19" + ], + "contributors": [ + "angelikatyborska" + ], + "files": { + "solution": [ + "lib/resistor_color.ex" + ], + "test": [ + "test/resistor_color_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Convert a resistor band's color to its numeric representation.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1458" +} diff --git a/elixir/resistor-color/.exercism/metadata.json b/elixir/resistor-color/.exercism/metadata.json new file mode 100644 index 0000000..7e78589 --- /dev/null +++ b/elixir/resistor-color/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"resistor-color","id":"fc40c77e3ca8429bab5959f239071895","url":"https://exercism.org/tracks/elixir/exercises/resistor-color","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/resistor-color/.formatter.exs b/elixir/resistor-color/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/resistor-color/.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/.gitignore b/elixir/resistor-color/.gitignore new file mode 100644 index 0000000..f87bc03 --- /dev/null +++ b/elixir/resistor-color/.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-*.tar + diff --git a/elixir/resistor-color/HELP.md b/elixir/resistor-color/HELP.md new file mode 100644 index 0000000..c7288f5 --- /dev/null +++ b/elixir/resistor-color/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.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/README.md b/elixir/resistor-color/README.md new file mode 100644 index 0000000..d0af14d --- /dev/null +++ b/elixir/resistor-color/README.md @@ -0,0 +1,60 @@ +# Resistor Color + +Welcome to Resistor Color 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. + +In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. + +These 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 + +The goal of this exercise is to create a way: + +- to look up the numerical value associated with a particular color band +- to list the different band colors + +Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: +Better Be Right Or Your Great Big Values Go Wrong. + +More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code]. + +[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code + +For this exercise, returning the list of colors is not required. + +## Source + +### Created by + +- @Bscruz19 + +### Contributed to by + +- @angelikatyborska + +### Based on + +Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458 \ No newline at end of file diff --git a/elixir/resistor-color/lib/resistor_color.ex b/elixir/resistor-color/lib/resistor_color.ex new file mode 100644 index 0000000..b032027 --- /dev/null +++ b/elixir/resistor-color/lib/resistor_color.ex @@ -0,0 +1,20 @@ +defmodule ResistorColor do + @doc """ + Return the value of a color band + """ + @color_codes %{ + black: 0, + brown: 1, + red: 2, + orange: 3, + yellow: 4, + green: 5, + blue: 6, + violet: 7, + grey: 8, + white: 9 + } + + @spec code(atom) :: integer() + def code(color), do: @color_codes[color] +end diff --git a/elixir/resistor-color/mix.exs b/elixir/resistor-color/mix.exs new file mode 100644 index 0000000..5c175d1 --- /dev/null +++ b/elixir/resistor-color/mix.exs @@ -0,0 +1,28 @@ +defmodule ResistorColor.MixProject do + use Mix.Project + + def project do + [ + app: :resistor_color, + 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/test/resistor_color_test.exs b/elixir/resistor-color/test/resistor_color_test.exs new file mode 100644 index 0000000..37b7529 --- /dev/null +++ b/elixir/resistor-color/test/resistor_color_test.exs @@ -0,0 +1,53 @@ +defmodule ResistorColorTest do + use ExUnit.Case + + # @tag :pending + test "returns black color code" do + assert ResistorColor.code(:black) == 0 + end + + # @tag :pending + test "returns brown color code" do + assert ResistorColor.code(:brown) == 1 + end + + # @tag :pending + test "returns red color code" do + assert ResistorColor.code(:red) == 2 + end + + # @tag :pending + test "returns orange color code" do + assert ResistorColor.code(:orange) == 3 + end + + # @tag :pending + test "returns yellow color code" do + assert ResistorColor.code(:yellow) == 4 + end + + # @tag :pending + test "returns green color code" do + assert ResistorColor.code(:green) == 5 + end + + # @tag :pending + test "returns blue color code" do + assert ResistorColor.code(:blue) == 6 + end + + # @tag :pending + test "returns violet color code" do + assert ResistorColor.code(:violet) == 7 + end + + # @tag :pending + test "returns grey color code" do + assert ResistorColor.code(:grey) == 8 + end + + # @tag :pending + test "returns white color code" do + assert ResistorColor.code(:white) == 9 + end +end diff --git a/elixir/resistor-color/test/test_helper.exs b/elixir/resistor-color/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/resistor-color/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)