Skip to content

Commit

Permalink
Add resistor-color-duo
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Sep 17, 2024
1 parent a25472c commit f839dd5
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "3678ff1c-79de-47bc-9219-80268cde53ce",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "reverse-string",
"name": "Reverse String",
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Instructions

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, and
brown-green-violet should return 15 too, ignoring the third color.
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/resistor_color_duo.erl"
],
"test": [
"test/resistor_color_duo_tests.erl"
],
"example": [
".meta/example.erl"
]
},
"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"
}
18 changes: 18 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-module(example).

-export([value/1]).


value([First, Second | _]) ->
color_code(First) * 10 + color_code(Second).

color_code(black) -> 0;
color_code(brown) -> 1;
color_code(red) -> 2;
color_code(orange) -> 3;
color_code(yellow) -> 4;
color_code(green) -> 5;
color_code(blue) -> 6;
color_code(violet) -> 7;
color_code(grey) -> 8;
color_code(white) -> 9.
31 changes: 31 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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"

[b7a6cbd2-ae3c-470a-93eb-56670b305640]
description = "White and red"

[77a8293d-2a83-4016-b1af-991acc12b9fe]
description = "Orange and orange"

[0c4fb44f-db7c-4d03-afa8-054350f156a8]
description = "Ignore additional colors"

[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
description = "Black and brown, one-digit"
30 changes: 30 additions & 0 deletions exercises/practice/resistor-color-duo/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%% Erlang compiler options
{erl_opts, [debug_info, warnings_as_errors]}.

{deps, [{erl_exercism, "0.1.2"}]}.

{dialyzer, [
{warnings, [underspecs, no_return]},
{get_warnings, true},
{plt_apps, top_level_deps}, % top_level_deps | all_deps
{plt_extra_apps, []},
{plt_location, local}, % local | "/my/file/name"
{plt_prefix, "rebar3"},
{base_plt_apps, [stdlib, kernel, crypto]},
{base_plt_location, global}, % global | "/my/file/name"
{base_plt_prefix, "rebar3"}
]}.

%% eunit:test(Tests)
{eunit_tests, []}.
%% Options for eunit:test(Tests, Opts)
{eunit_opts, [verbose]}.

%% == xref ==

{xref_warnings, true}.

%% xref checks to run
{xref_checks, [undefined_function_calls, undefined_functions,
locals_not_used, exports_not_used,
deprecated_function_calls, deprecated_functions]}.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{application, resistor_color_duo,
[{description, "exercism.io - resistor color duo"},
{vsn, "0.0.1"},
{modules, []},
{registered, []},
{applications, [kernel,
stdlib]},
{env, []}
]}.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(resistor_color_duo).

-export([value/1]).


value(_Colors) ->
undefined.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-module(resistor_color_duo_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_brown_and_black_test_'() ->
{"brown and black",
?_assertMatch(10, resistor_color_duo:value([brown, black]))}.

'2_blue_and_grey_test_'() ->
{"blue and grey",
?_assertMatch(68, resistor_color_duo:value([blue, grey]))}.

'3_yellow_and_violet_test_'() ->
{"yellow and violet",
?_assertMatch(47, resistor_color_duo:value([yellow, violet]))}.

'4_white_and_red_test_'() ->
{"white and red",
?_assertMatch(92, resistor_color_duo:value([white, red]))}.

'5_orange_and_orange_test_'() ->
{"orange and orange",
?_assertMatch(33, resistor_color_duo:value([orange, orange]))}.

'6_ignore_additional_colors_test_'() ->
{"ignore additional colors",
?_assertMatch(51, resistor_color_duo:value([green, brown, orange]))}.

'7_black_and_brown_one_digit_test_'() ->
{"black and brown, one-digit",
?_assertMatch(1, resistor_color_duo:value([black, brown]))}.

0 comments on commit f839dd5

Please sign in to comment.