Skip to content

Commit

Permalink
Add resistor-color-trio exercise (#794)
Browse files Browse the repository at this point in the history
* Add `resistor-color-trio` exercise

* Update exercises/practice/resistor-color-trio/src/resistor_color_trio.clj

Co-authored-by: Anastasios Chatzialexiou <[email protected]>

* Tweaks

---------

Co-authored-by: Anastasios Chatzialexiou <[email protected]>
  • Loading branch information
ErikSchierboom and tasxatzial authored Jan 22, 2025
1 parent 01802d5 commit 7398479
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,19 @@
],
"difficulty": 5
},
{
"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",
Expand Down
56 changes: 56 additions & 0 deletions exercises/practice/resistor-color-trio/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
25 changes: 25 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -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 resistor-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))))
4 changes: 4 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns resistor-color-trio-generator)

(defn update-test-case [test-case]
(update test-case :expected #(str (:value %) " " (:unit %))))
9 changes: 9 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns resistor-color-trio-test
(:require [clojure.test :refer [deftest testing is]]
resistor-color-trio))

{{#test_cases.label}}
(deftest resistor-label_test_{{idx}}
(testing {{description}}
(is (= {{expected}} (resistor-color-trio/resistor-label {{input.colors}})))))
{{/test_cases.label}}
40 changes: 40 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions exercises/practice/resistor-color-trio/deps.edn
Original file line number Diff line number Diff line change
@@ -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}}}
4 changes: 4 additions & 0 deletions exercises/practice/resistor-color-trio/project.clj
Original file line number Diff line number Diff line change
@@ -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"]])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns resistor-color-trio)

(defn resistor-label
"Returns the resistor label based on the given color bands."
[colors]
;; function body
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns resistor-color-trio-test
(:require [clojure.test :refer [deftest testing is]]
resistor-color-trio))

(deftest resistor-label_test_1
(testing "Orange and orange and black"
(is (= "33 ohms" (resistor-color-trio/resistor-label ["orange" "orange" "black"])))))

(deftest resistor-label_test_2
(testing "Blue and grey and brown"
(is (= "680 ohms" (resistor-color-trio/resistor-label ["blue" "grey" "brown"])))))

(deftest resistor-label_test_3
(testing "Red and black and red"
(is (= "2 kiloohms" (resistor-color-trio/resistor-label ["red" "black" "red"])))))

(deftest resistor-label_test_4
(testing "Green and brown and orange"
(is (= "51 kiloohms" (resistor-color-trio/resistor-label ["green" "brown" "orange"])))))

(deftest resistor-label_test_5
(testing "Yellow and violet and yellow"
(is (= "470 kiloohms" (resistor-color-trio/resistor-label ["yellow" "violet" "yellow"])))))

(deftest resistor-label_test_6
(testing "Blue and violet and blue"
(is (= "67 megaohms" (resistor-color-trio/resistor-label ["blue" "violet" "blue"])))))

(deftest resistor-label_test_7
(testing "Minimum possible value"
(is (= "0 ohms" (resistor-color-trio/resistor-label ["black" "black" "black"])))))

(deftest resistor-label_test_8
(testing "Maximum possible value"
(is (= "99 gigaohms" (resistor-color-trio/resistor-label ["white" "white" "white"])))))

(deftest resistor-label_test_9
(testing "First two colors make an invalid octal number"
(is (= "8 ohms" (resistor-color-trio/resistor-label ["black" "grey" "black"])))))

(deftest resistor-label_test_10
(testing "Ignore extra colors"
(is (= "650 kiloohms" (resistor-color-trio/resistor-label ["blue" "green" "yellow" "orange"])))))

0 comments on commit 7398479

Please sign in to comment.