🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Resistor Color in the Elixir Track

Published at Jan 12 2020 · 0 comments
Instructions
Test suite
Solution

Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

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

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.

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been 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:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

Source

This is an exercise to introduce users to basic programming constructs, just after Hello World. Electronic color code Wikipedia article

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

resistor_color_test.exs

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

  @tag :pending
  test "returns all colors" do
    colors = [
      "black",
      "brown",
      "red",
      "orange",
      "yellow",
      "green",
      "blue",
      "violet",
      "grey",
      "white"
    ]

    assert ResistorColor.colors() == colors
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule ResistorColor do
  @moduledoc false

  @colors_to_values %{
    "black" => 0,
    "brown" => 1,
    "red" => 2,
    "orange" => 3,
    "yellow" => 4,
    "green" => 5,
    "blue" => 6,
    "violet" => 7,
    "grey" => 8,
    "white" => 9
  }

  @spec colors() :: list(String.t())
  def colors do
    @colors_to_values
    |> Map.to_list()
    |> Enum.sort_by(&elem(&1, 1))
    |> Enum.map(fn {color, _value} -> color end)
  end

  @spec code(String.t()) :: integer()
  def code(color) do
    @colors_to_values[color]
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?