Skip to content

Commit f625dfe

Browse files
committed
New practice exercise
1 parent c867cf7 commit f625dfe

File tree

10 files changed

+204
-1
lines changed

10 files changed

+204
-1
lines changed

config.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2661,7 +2661,22 @@
26612661
"bitstrings"
26622662
],
26632663
"difficulty": 6
2664-
}
2664+
},
2665+
{
2666+
"slug": "resistor-color-duo",
2667+
"name": "Resistor Color Duo",
2668+
"uuid": "92387ac7-4c6d-4568-819e-e037b1d86a72",
2669+
"prerequisites": [
2670+
"atoms",
2671+
"lists",
2672+
"pattern-matching",
2673+
"maps"
2674+
],
2675+
"practices": [
2676+
"maps"
2677+
],
2678+
"difficulty": 2
2679+
}
26652680
],
26662681
"foregone": [
26672682
"linked-list",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Description
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
* Each resistor has a resistance value.
7+
* Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
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.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- Black: 0
21+
- Brown: 1
22+
- Red: 2
23+
- Orange: 3
24+
- Yellow: 4
25+
- Green: 5
26+
- Blue: 6
27+
- Violet: 7
28+
- Grey: 8
29+
- White: 9
30+
31+
From the example above:
32+
brown-green should return 15
33+
brown-green-violet should return 15 too, ignoring the third color.
34+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": ["jiegillet"],
3+
"contributors": [],
4+
"files": {
5+
"example": [
6+
".meta/example.ex"
7+
],
8+
"solution": [
9+
"lib/resistor_color_duo.ex"
10+
],
11+
"test": [
12+
"test/resistor_color_duo_test.exs"
13+
]
14+
},
15+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
16+
"source": "Maud de Vries, Erik Schierboom",
17+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule ResistorColorDuo do
2+
@colors %{
3+
black: 0,
4+
brown: 1,
5+
red: 2,
6+
orange: 3,
7+
yellow: 4,
8+
green: 5,
9+
blue: 6,
10+
violet: 7,
11+
grey: 8,
12+
white: 9
13+
}
14+
15+
@doc """
16+
Calculate a resistance value from two colors
17+
"""
18+
@spec value(colors :: [atom]) :: integer
19+
def value([a, b | _]) do
20+
10 * @colors[a] + @colors[b]
21+
end
22+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
12+
description = "Brown and black"
13+
14+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
15+
description = "Blue and grey"
16+
17+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
18+
description = "Yellow and violet"
19+
20+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
21+
description = "Orange and orange"
22+
23+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
24+
description = "Ignore additional colors"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule ResistorColorDuo do
2+
@doc """
3+
Calculate a resistance value from two colors
4+
"""
5+
@spec value(colors :: [atom]) :: integer
6+
def value(colors) do
7+
end
8+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule ResistorColorDuo.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :resistor_color_duo,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule ResistorColorDuoTest do
2+
use ExUnit.Case
3+
4+
# @tag :pending
5+
test "Brown and black" do
6+
colors = [:brown, :black]
7+
output = ResistorColorDuo.value(colors)
8+
expected = 10
9+
10+
assert output == expected
11+
end
12+
13+
@tag :pending
14+
test "Blue and grey" do
15+
colors = [:blue, :grey]
16+
output = ResistorColorDuo.value(colors)
17+
expected = 68
18+
19+
assert output == expected
20+
end
21+
22+
@tag :pending
23+
test "Yellow and violet" do
24+
colors = [:yellow, :violet]
25+
output = ResistorColorDuo.value(colors)
26+
expected = 47
27+
28+
assert output == expected
29+
end
30+
31+
@tag :pending
32+
test "Orange and orange" do
33+
colors = [:orange, :orange]
34+
output = ResistorColorDuo.value(colors)
35+
expected = 33
36+
37+
assert output == expected
38+
end
39+
40+
@tag :pending
41+
test "Ignore additional colors" do
42+
colors = [:green, :brown, :orange]
43+
output = ResistorColorDuo.value(colors)
44+
expected = 51
45+
46+
assert output == expected
47+
end
48+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ExUnit.start()
2+
ExUnit.configure(exclude: :pending, trace: true)

0 commit comments

Comments
 (0)