diff --git a/config.json b/config.json index f9cadc0b..cfe79798 100644 --- a/config.json +++ b/config.json @@ -1007,6 +1007,17 @@ "math" ] }, + { + "slug": "resistor-color-duo", + "name": "Resistor Color Duo", + "uuid": "6d127442-d181-4654-8aec-36d42f09a949", + "practices": [], + "prerequisites": [ + "vectors", + "numbers" + ], + "difficulty": 3 + }, { "slug": "proverb", "name": "Proverb", diff --git a/exercises/practice/resistor-color-duo/.docs/instructions.md b/exercises/practice/resistor-color-duo/.docs/instructions.md new file mode 100644 index 00000000..4ae694da --- /dev/null +++ b/exercises/practice/resistor-color-duo/.docs/instructions.md @@ -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. diff --git a/exercises/practice/resistor-color-duo/.meta/config.json b/exercises/practice/resistor-color-duo/.meta/config.json new file mode 100644 index 00000000..377cee8b --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "src/resistor_color_duo.clj" + ], + "test": [ + "test/resistor_color_duo_test.clj" + ], + "example": [ + ".meta/example.clj" + ] + }, + "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" +} diff --git a/exercises/practice/resistor-color-duo/.meta/example.clj b/exercises/practice/resistor-color-duo/.meta/example.clj new file mode 100644 index 00000000..fbd4cb61 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/example.clj @@ -0,0 +1,10 @@ +(ns resistor-color-duo) + +(def colors ["black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"]) + +(defn- color-code [color] (.indexOf colors color)) + +(defn resistor-value + "Returns the resistor value based on the given colors" + [[color-1 color-2]] + (+ (* 10 (color-code color-1)) (color-code color-2))) diff --git a/exercises/practice/resistor-color-duo/.meta/generator.tpl b/exercises/practice/resistor-color-duo/.meta/generator.tpl new file mode 100644 index 00000000..066dce72 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/generator.tpl @@ -0,0 +1,8 @@ +(ns resistor-color-duo-test + (:require [clojure.test :refer [deftest testing is]] + resistor-color-duo)) +{{#test_cases.value}} +(deftest resistor-value_test_{{idx}} + (testing {{description}} + (is (= {{expected}} (resistor-color-duo/resistor-value {{input.colors}}))))) +{{/test_cases.value~}} \ No newline at end of file diff --git a/exercises/practice/resistor-color-duo/.meta/tests.toml b/exercises/practice/resistor-color-duo/.meta/tests.toml new file mode 100644 index 00000000..9036fc78 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/tests.toml @@ -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" diff --git a/exercises/practice/resistor-color-duo/deps.edn b/exercises/practice/resistor-color-duo/deps.edn new file mode 100644 index 00000000..561c3e2d --- /dev/null +++ b/exercises/practice/resistor-color-duo/deps.edn @@ -0,0 +1,6 @@ +{:aliases {:test {:extra-paths ["test"] + :extra-deps {io.github.cognitect-labs/test-runner + {:git/url "https://github.com/cognitect-labs/test-runner.git" + :sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}} + :main-opts ["-m" "cognitect.test-runner"] + :exec-fn cognitect.test-runner.api/test}}} \ No newline at end of file diff --git a/exercises/practice/resistor-color-duo/project.clj b/exercises/practice/resistor-color-duo/project.clj new file mode 100644 index 00000000..36ff7921 --- /dev/null +++ b/exercises/practice/resistor-color-duo/project.clj @@ -0,0 +1,4 @@ +(defproject resistor-color-duo "0.1.0-SNAPSHOT" + :description "resistor-color-duo exercise." + :url "https://github.com/exercism/clojure/tree/main/exercises/practice/resistor-color-duo" + :dependencies [[org.clojure/clojure "1.11.1"]]) diff --git a/exercises/practice/resistor-color-duo/src/resistor_color_duo.clj b/exercises/practice/resistor-color-duo/src/resistor_color_duo.clj new file mode 100644 index 00000000..f825b8fc --- /dev/null +++ b/exercises/practice/resistor-color-duo/src/resistor_color_duo.clj @@ -0,0 +1,7 @@ +(ns resistor-color-duo) + +(defn resistor-value + "Returns the resistor value based on the given colors" + [colors] + ;; function body + ) diff --git a/exercises/practice/resistor-color-duo/test/resistor_color_duo_test.clj b/exercises/practice/resistor-color-duo/test/resistor_color_duo_test.clj new file mode 100644 index 00000000..907fd6f1 --- /dev/null +++ b/exercises/practice/resistor-color-duo/test/resistor_color_duo_test.clj @@ -0,0 +1,31 @@ +(ns resistor-color-duo-test + (:require [clojure.test :refer [deftest testing is]] + resistor-color-duo)) + +(deftest resistor-value_test_1 + (testing "Brown and black" + (is (= 10 (resistor-color-duo/resistor-value ["brown" "black"]))))) + +(deftest resistor-value_test_2 + (testing "Blue and grey" + (is (= 68 (resistor-color-duo/resistor-value ["blue" "grey"]))))) + +(deftest resistor-value_test_3 + (testing "Yellow and violet" + (is (= 47 (resistor-color-duo/resistor-value ["yellow" "violet"]))))) + +(deftest resistor-value_test_4 + (testing "White and red" + (is (= 92 (resistor-color-duo/resistor-value ["white" "red"]))))) + +(deftest resistor-value_test_5 + (testing "Orange and orange" + (is (= 33 (resistor-color-duo/resistor-value ["orange" "orange"]))))) + +(deftest resistor-value_test_6 + (testing "Ignore additional colors" + (is (= 51 (resistor-color-duo/resistor-value ["green" "brown" "orange"]))))) + +(deftest resistor-value_test_7 + (testing "Black and brown, one-digit" + (is (= 1 (resistor-color-duo/resistor-value ["black" "brown"])))))