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:
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.
Execute the tests with:
$ mix test
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.
This is an exercise to introduce users to basic programming constructs, just after Hello World. Electronic color code Wikipedia article
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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
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
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments