diff --git a/config.json b/config.json index 19322079..6f17fac2 100644 --- a/config.json +++ b/config.json @@ -1253,6 +1253,19 @@ "time" ] }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "4f8bc34b-59bd-4a93-a120-d5e5ee3bd4a3", + "practices": [], + "prerequisites": [ + "numbers", + "vectors", + "strings", + "conditionals" + ], + "difficulty": 5 + }, { "slug": "list-ops", "name": "List Ops", diff --git a/exercises/practice/resistor-color-trio/.docs/instructions.md b/exercises/practice/resistor-color-trio/.docs/instructions.md new file mode 100644 index 00000000..1ac5cf5e --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.md @@ -0,0 +1,56 @@ +# Instructions + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. +For this exercise, you need to know only three 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 acts as a digit of a 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 3 colors as input, and outputs the correct value, in ohms. + The color bands are encoded as follows: + +- black: 0 +- brown: 1 +- red: 2 +- orange: 3 +- yellow: 4 +- green: 5 +- blue: 6 +- violet: 7 +- grey: 8 +- white: 9 + +In Resistor Color Duo you decoded the first two colors. +For instance: orange-orange got the main value `33`. +The third color stands for how many zeros need to be added to the main value. +The main value plus the zeros gives us a value in ohms. +For the exercise it doesn't matter what ohms really are. +For example: + +- orange-orange-black would be 33 and no zeros, which becomes 33 ohms. +- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms. +- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms. + +(If Math is your thing, you may want to think of the zeros as exponents of 10. +If Math is not your thing, go with the zeros. +It really is the same thing, just in plain English instead of Math lingo.) + +This exercise is about translating the colors into a label: + +> "... ohms" + +So an input of `"orange", "orange", "black"` should return: + +> "33 ohms" + +When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms". +That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams". + +For example, an input of `"orange", "orange", "orange"` should return: + +> "33 kiloohms" + +[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix diff --git a/exercises/practice/resistor-color-trio/.meta/config.json b/exercises/practice/resistor-color-trio/.meta/config.json new file mode 100644 index 00000000..01a2440e --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "src/resistor_color_trio.clj" + ], + "test": [ + "test/resistor_color_trio_test.clj" + ], + "example": [ + ".meta/example.clj" + ] + }, + "blurb": "Convert color codes, as used on resistors, to a human-readable label.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1549" +} diff --git a/exercises/practice/resistor-color-trio/.meta/example.clj b/exercises/practice/resistor-color-trio/.meta/example.clj new file mode 100644 index 00000000..bb133b79 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/example.clj @@ -0,0 +1,25 @@ +(ns resistor-color-trio) + +(def colors ["black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"]) + +(defn- color->value [color] (.indexOf colors color)) + +(defn- pow [a b] (reduce * (repeat b a))) + +(defn- colors->value [colors] + (let [[value-1 value-2 value-3] (map color->value colors)] + (* (+ (* 10 value-1) value-2) (pow 10 value-3)))) + +(def units [nil "kilo" "mega" "giga"]) + +(defn unit-label [[amount unit]] (str amount " " unit "ohms")) + +(defn label [colors] + (let [value (colors->value colors)] + (->> units + (map-indexed vector) + (some + (fn [[idx unit]] + (when (<= value (pow 1000 (inc idx))) + [(quot value (pow 1000 idx)) unit]))) + (unit-label)))) diff --git a/exercises/practice/resistor-color-trio/.meta/generator.clj b/exercises/practice/resistor-color-trio/.meta/generator.clj new file mode 100644 index 00000000..03969fd4 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/generator.clj @@ -0,0 +1,4 @@ +(ns resistor-color-trio-generator) + +(defn update-test-case [test-case] + (update test-case :expected #(str (:value %) " " (:unit %)))) diff --git a/exercises/practice/resistor-color-trio/.meta/generator.tpl b/exercises/practice/resistor-color-trio/.meta/generator.tpl new file mode 100644 index 00000000..b0914a20 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/generator.tpl @@ -0,0 +1,9 @@ +(ns resistor-color-trio-test + (:require [clojure.test :refer [deftest testing is]] + resistor-color-trio)) + +{{#test_cases.label}} +(deftest label_test_{{idx}} + (testing {{description}} + (is (= {{expected}} (resistor-color-trio/label {{input.colors}}))))) +{{/test_cases.label}} diff --git a/exercises/practice/resistor-color-trio/.meta/tests.toml b/exercises/practice/resistor-color-trio/.meta/tests.toml new file mode 100644 index 00000000..b7d45fa5 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/tests.toml @@ -0,0 +1,40 @@ +# 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. + +[d6863355-15b7-40bb-abe0-bfb1a25512ed] +description = "Orange and orange and black" + +[1224a3a9-8c8e-4032-843a-5224e04647d6] +description = "Blue and grey and brown" + +[b8bda7dc-6b95-4539-abb2-2ad51d66a207] +description = "Red and black and red" + +[5b1e74bc-d838-4eda-bbb3-eaba988e733b] +description = "Green and brown and orange" + +[f5d37ef9-1919-4719-a90d-a33c5a6934c9] +description = "Yellow and violet and yellow" + +[5f6404a7-5bb3-4283-877d-3d39bcc33854] +description = "Blue and violet and blue" + +[7d3a6ab8-e40e-46c3-98b1-91639fff2344] +description = "Minimum possible value" + +[ca0aa0ac-3825-42de-9f07-dac68cc580fd] +description = "Maximum possible value" + +[0061a76c-903a-4714-8ce2-f26ce23b0e09] +description = "First two colors make an invalid octal number" + +[30872c92-f567-4b69-a105-8455611c10c4] +description = "Ignore extra colors" diff --git a/exercises/practice/resistor-color-trio/deps.edn b/exercises/practice/resistor-color-trio/deps.edn new file mode 100644 index 00000000..561c3e2d --- /dev/null +++ b/exercises/practice/resistor-color-trio/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-trio/project.clj b/exercises/practice/resistor-color-trio/project.clj new file mode 100644 index 00000000..93db3da9 --- /dev/null +++ b/exercises/practice/resistor-color-trio/project.clj @@ -0,0 +1,4 @@ +(defproject resistor-color-trio "0.1.0-SNAPSHOT" + :description "resistor-color-trio exercise." + :url "https://github.com/exercism/clojure/tree/main/exercises/practice/resistor-color-trio" + :dependencies [[org.clojure/clojure "1.11.1"]]) diff --git a/exercises/practice/resistor-color-trio/src/resistor_color_trio.clj b/exercises/practice/resistor-color-trio/src/resistor_color_trio.clj new file mode 100644 index 00000000..54d9f125 --- /dev/null +++ b/exercises/practice/resistor-color-trio/src/resistor_color_trio.clj @@ -0,0 +1,7 @@ +(ns resistor-color-trio) + +(defn label + "Returns the label for band of resistor colors." + [colors] + ;; function body + ) diff --git a/exercises/practice/resistor-color-trio/test/resistor_color_trio_test.clj b/exercises/practice/resistor-color-trio/test/resistor_color_trio_test.clj new file mode 100644 index 00000000..40e2ec64 --- /dev/null +++ b/exercises/practice/resistor-color-trio/test/resistor_color_trio_test.clj @@ -0,0 +1,43 @@ +(ns resistor-color-trio-test + (:require [clojure.test :refer [deftest testing is]] + resistor-color-trio)) + +(deftest label_test_1 + (testing "Orange and orange and black" + (is (= "33 ohms" (resistor-color-trio/label ["orange" "orange" "black"]))))) + +(deftest label_test_2 + (testing "Blue and grey and brown" + (is (= "680 ohms" (resistor-color-trio/label ["blue" "grey" "brown"]))))) + +(deftest label_test_3 + (testing "Red and black and red" + (is (= "2 kiloohms" (resistor-color-trio/label ["red" "black" "red"]))))) + +(deftest label_test_4 + (testing "Green and brown and orange" + (is (= "51 kiloohms" (resistor-color-trio/label ["green" "brown" "orange"]))))) + +(deftest label_test_5 + (testing "Yellow and violet and yellow" + (is (= "470 kiloohms" (resistor-color-trio/label ["yellow" "violet" "yellow"]))))) + +(deftest label_test_6 + (testing "Blue and violet and blue" + (is (= "67 megaohms" (resistor-color-trio/label ["blue" "violet" "blue"]))))) + +(deftest label_test_7 + (testing "Minimum possible value" + (is (= "0 ohms" (resistor-color-trio/label ["black" "black" "black"]))))) + +(deftest label_test_8 + (testing "Maximum possible value" + (is (= "99 gigaohms" (resistor-color-trio/label ["white" "white" "white"]))))) + +(deftest label_test_9 + (testing "First two colors make an invalid octal number" + (is (= "8 ohms" (resistor-color-trio/label ["black" "grey" "black"]))))) + +(deftest label_test_10 + (testing "Ignore extra colors" + (is (= "650 kiloohms" (resistor-color-trio/label ["blue" "green" "yellow" "orange"])))))