From 70eb016cc2fc8463f71fbd9626e1c20a05643461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:31:19 -0700 Subject: [PATCH] Add resistor-color exercise --- config.json | 8 ++++ .../resistor-color/.docs/instructions.md | 39 +++++++++++++++++++ exercises/practice/resistor-color/.gitignore | 11 ++++++ .../practice/resistor-color/.meta/config.json | 19 +++++++++ .../practice/resistor-color/.meta/example.lfe | 27 +++++++++++++ .../practice/resistor-color/.meta/tests.toml | 22 +++++++++++ exercises/practice/resistor-color/Makefile | 21 ++++++++++ .../practice/resistor-color/rebar.config | 11 ++++++ exercises/practice/resistor-color/rebar.lock | 8 ++++ .../resistor-color/src/resistor-color.app.src | 11 ++++++ .../resistor-color/src/resistor-color.lfe | 4 ++ .../test/resistor-color-tests.lfe | 27 +++++++++++++ 12 files changed, 208 insertions(+) create mode 100644 exercises/practice/resistor-color/.docs/instructions.md create mode 100644 exercises/practice/resistor-color/.gitignore create mode 100644 exercises/practice/resistor-color/.meta/config.json create mode 100644 exercises/practice/resistor-color/.meta/example.lfe create mode 100644 exercises/practice/resistor-color/.meta/tests.toml create mode 100644 exercises/practice/resistor-color/Makefile create mode 100644 exercises/practice/resistor-color/rebar.config create mode 100644 exercises/practice/resistor-color/rebar.lock create mode 100644 exercises/practice/resistor-color/src/resistor-color.app.src create mode 100644 exercises/practice/resistor-color/src/resistor-color.lfe create mode 100644 exercises/practice/resistor-color/test/resistor-color-tests.lfe diff --git a/config.json b/config.json index f59bb289..4d7ad27c 100644 --- a/config.json +++ b/config.json @@ -172,6 +172,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "2af13bc4-76d6-4986-988f-b6acce6aa128", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "queen-attack", "name": "Queen Attack", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 00000000..646c1439 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.md @@ -0,0 +1,39 @@ +# 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. + +In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. + +These 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 + +The goal of this exercise is to create a way: + +- to look up the numerical value associated with a particular color band +- to list the different band colors + +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. + +More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code]. + +[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code diff --git a/exercises/practice/resistor-color/.gitignore b/exercises/practice/resistor-color/.gitignore new file mode 100644 index 00000000..6dd20ff0 --- /dev/null +++ b/exercises/practice/resistor-color/.gitignore @@ -0,0 +1,11 @@ +## -*- conf -*- +.rebar3 +_build/ +ebin/ +erl_crash.dump +rebar3.crashdump + +tmp +bin/configlet +bin/configlet.exe +CHECKLIST diff --git a/exercises/practice/resistor-color/.meta/config.json b/exercises/practice/resistor-color/.meta/config.json new file mode 100644 index 00000000..ca621595 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/resistor-color.lfe" + ], + "test": [ + "test/resistor-color-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ] + }, + "blurb": "Convert a resistor band's color to its numeric representation.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1458" +} diff --git a/exercises/practice/resistor-color/.meta/example.lfe b/exercises/practice/resistor-color/.meta/example.lfe new file mode 100644 index 00000000..1172bfac --- /dev/null +++ b/exercises/practice/resistor-color/.meta/example.lfe @@ -0,0 +1,27 @@ +(defmodule resistor-color + (export (color-code 1) (colors 0))) + +(defun color-code + (("black") 0) + (("brown") 1) + (("red") 2) + (("orange") 3) + (("yellow") 4) + (("green") 5) + (("blue") 6) + (("violet") 7) + (("grey") 8) + (("white") 9) + ((_) 'false)) + +(defun colors () + '("black" + "brown" + "red" + "orange" + "yellow" + "green" + "blue" + "violet" + "grey" + "white")) diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml new file mode 100644 index 00000000..9d4ee973 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/tests.toml @@ -0,0 +1,22 @@ +# 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. + +[49eb31c5-10a8-4180-9f7f-fea632ab87ef] +description = "Color codes -> Black" + +[0a4df94b-92da-4579-a907-65040ce0b3fc] +description = "Color codes -> White" + +[5f81608d-f36f-4190-8084-f45116b6f380] +description = "Color codes -> Orange" + +[581d68fa-f968-4be2-9f9d-880f2fb73cf7] +description = "Colors" diff --git a/exercises/practice/resistor-color/Makefile b/exercises/practice/resistor-color/Makefile new file mode 100644 index 00000000..fbb5a7de --- /dev/null +++ b/exercises/practice/resistor-color/Makefile @@ -0,0 +1,21 @@ +ERL := $(shell which erl) +REBAR3 := $(shell which rebar3) + +null := +space := $(null) # +comma := , + +ifeq ($(ERL),) + $(error Can't find Erlang executable 'erl') +else ifeq ($(REBAR3),) + $(error Can't find rebar3) +endif + +compile: ; $(REBAR3) compile + +clean: ; $(REBAR3) clean + +.PHONY: test +test: + $(REBAR3) eunit \ + -m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) diff --git a/exercises/practice/resistor-color/rebar.config b/exercises/practice/resistor-color/rebar.config new file mode 100644 index 00000000..d53487ac --- /dev/null +++ b/exercises/practice/resistor-color/rebar.config @@ -0,0 +1,11 @@ +{plugins, [{rebar3_lfe, "0.4.3"}]}. + +{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. + +{deps, [{lfe, "2.1.1"}]}. + +{profiles, + [{test, + [{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, + {deps, + [{ltest, "0.13.3"}]}]}]}. diff --git a/exercises/practice/resistor-color/rebar.lock b/exercises/practice/resistor-color/rebar.lock new file mode 100644 index 00000000..d5a6b3b9 --- /dev/null +++ b/exercises/practice/resistor-color/rebar.lock @@ -0,0 +1,8 @@ +{"1.2.0", +[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}. +[ +{pkg_hash,[ + {<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]}, +{pkg_hash_ext,[ + {<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]} +]. diff --git a/exercises/practice/resistor-color/src/resistor-color.app.src b/exercises/practice/resistor-color/src/resistor-color.app.src new file mode 100644 index 00000000..11798b5c --- /dev/null +++ b/exercises/practice/resistor-color/src/resistor-color.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'resistor-color', + [{description, ""}, + {vsn, "0.0.1"}, + {modules, + ['resistor-color']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/resistor-color/src/resistor-color.lfe b/exercises/practice/resistor-color/src/resistor-color.lfe new file mode 100644 index 00000000..33111b18 --- /dev/null +++ b/exercises/practice/resistor-color/src/resistor-color.lfe @@ -0,0 +1,4 @@ +(defmodule resistor-color + (export (color-code 1) (colors 0))) + + ; Please implement the color-code and colors functions. \ No newline at end of file diff --git a/exercises/practice/resistor-color/test/resistor-color-tests.lfe b/exercises/practice/resistor-color/test/resistor-color-tests.lfe new file mode 100644 index 00000000..7e045c5c --- /dev/null +++ b/exercises/practice/resistor-color/test/resistor-color-tests.lfe @@ -0,0 +1,27 @@ +(defmodule resistor-color-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest color-codes-black + (is-equal 0 (resistor-color:color-code "black"))) + +(deftest color-codes-white + (is-equal 9 (resistor-color:color-code "white"))) + +(deftest color-codes-orange + (is-equal 3 (resistor-color:color-code "orange"))) + +(deftest colors + (is-equal '("black" + "brown" + "red" + "orange" + "yellow" + "green" + "blue" + "violet" + "grey" + "white") + (resistor-color:colors)))