Skip to content

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

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,21 @@
],
"difficulty": 6
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "92387ac7-4c6d-4568-819e-e037b1d86a72",
"prerequisites": [
"atoms",
"lists",
"pattern-matching",
"maps"
],
"practices": [
"maps"
],
Comment on lines +2669 to +2677
Copy link
Member

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 before resistor-color because resistor-color has an implementation with a list and Enum.find_index(@colors, &(&1 == color)), so it requires enum, and enum comes after all those prerequisites.

We cannot edit resistor-color to NOT use enum 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 than resistor-color-duo, and that resistor-color-duo is easier than resistor-color-trio 🤔

Copy link
Contributor Author

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 after lists and enum 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lists and maps are required by enum.

I think using a list for this exercise would make it less idiomatic. I guess we need to accept it like it is :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map keys aren't ordered.

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.

Copy link
Contributor Author

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 is O(1) rather than O(n).

"difficulty": 2
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
Expand Down
34 changes: 34 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
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.

4 changes: 4 additions & 0 deletions exercises/practice/resistor-color-duo/.formatter.exs
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}"]
]
18 changes: 18 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
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"
}
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.ex
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
Copy link
Member

Choose a reason for hiding this comment

The 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 resistor-color to also use atoms for the colors? Now they're strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def value([a, b | _]) do
10 * @colors[a] + @colors[b]
end
end
24 changes: 24 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
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
28 changes: 28 additions & 0 deletions exercises/practice/resistor-color-duo/mix.exs
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
2 changes: 2 additions & 0 deletions exercises/practice/resistor-color-duo/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)