-
-
Notifications
You must be signed in to change notification settings - Fork 402
New practice exercise resistor-color-duo
#800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Description | ||
|
||
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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": ["jiegillet"], | ||
"contributors": [], | ||
"files": { | ||
"example": [ | ||
".meta/example.ex" | ||
], | ||
"solution": [ | ||
"lib/resistor_color_duo.ex" | ||
], | ||
"test": [ | ||
"test/resistor_color_duo_test.exs" | ||
] | ||
}, | ||
"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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule ResistorColorDuo do | ||
@colors %{ | ||
black: 0, | ||
brown: 1, | ||
red: 2, | ||
orange: 3, | ||
yellow: 4, | ||
green: 5, | ||
blue: 6, | ||
violet: 7, | ||
grey: 8, | ||
white: 9 | ||
} | ||
|
||
@doc """ | ||
Calculate a resistance value from two colors | ||
""" | ||
@spec value(colors :: [atom]) :: integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that the colors are atoms. What do you think about editing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave it to me. Maybe we should make the trio all a little bit more consistent on other things as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
def value([a, b | _]) do | ||
10 * @colors[a] + @colors[b] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
[ce11995a-5b93-4950-a5e9-93423693b2fc] | ||
description = "Brown and black" | ||
|
||
[7bf82f7a-af23-48ba-a97d-38d59406a920] | ||
description = "Blue and grey" | ||
|
||
[f1886361-fdfd-4693-acf8-46726fe24e0c] | ||
description = "Yellow and violet" | ||
|
||
[77a8293d-2a83-4016-b1af-991acc12b9fe] | ||
description = "Orange and orange" | ||
|
||
[0c4fb44f-db7c-4d03-afa8-054350f156a8] | ||
description = "Ignore additional colors" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule ResistorColorDuo do | ||
@doc """ | ||
Calculate a resistance value from two colors | ||
""" | ||
@spec value(colors :: [atom]) :: integer | ||
def value(colors) do | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule ResistorColorDuoTest do | ||
use ExUnit.Case | ||
|
||
# @tag :pending | ||
test "Brown and black" do | ||
colors = [:brown, :black] | ||
output = ResistorColorDuo.value(colors) | ||
expected = 10 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Blue and grey" do | ||
colors = [:blue, :grey] | ||
output = ResistorColorDuo.value(colors) | ||
expected = 68 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Yellow and violet" do | ||
colors = [:yellow, :violet] | ||
output = ResistorColorDuo.value(colors) | ||
expected = 47 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Orange and orange" do | ||
colors = [:orange, :orange] | ||
output = ResistorColorDuo.value(colors) | ||
expected = 33 | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Ignore additional colors" do | ||
colors = [:green, :brown, :orange] | ||
output = ResistorColorDuo.value(colors) | ||
expected = 51 | ||
|
||
assert output == expected | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. With those prerequisites,
resistor-color-duo
would be unlocked beforeresistor-color
becauseresistor-color
has an implementation with a list andEnum.find_index(@colors, &(&1 == color))
, so it requiresenum
, andenum
comes after all those prerequisites.We cannot edit
resistor-color
to NOT useenum
because it needs to return all the colors in order, and map keys aren't ordered.I don't know if that's a problem, but it defies my expectations. I would think that
resistor-color
is easier thanresistor-color-duo
, and thatresistor-color-duo
is easier thanresistor-color-trio
🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this one has
maps
, does that come afterlists
andenum
or is it a different branch?I could use a list implementation to make them all consistent. To be honest I didn't notice
resistor-color
existed haha.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lists
andmaps
are required byenum
.I think using a list for this exercise would make it less idiomatic. I guess we need to accept it like it is :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100% agree with this statement, it is a common fallacy for students to believe that map keys are ordered just because they appear so in small maps.
Not sure if this is a problem though. This is somewhat similar to the "Instruments of Texas" theme that we reuse in the concept exercises as Maud had designed these exercises as teaching exercises. Each one just teaches something different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Someone going through all three exercises in a row is likely to reuse the same implementation though. Either with maps or lists. I probably would have written
resistor-color
with maps as well since lookup isO(1)
rather thanO(n)
.