Skip to content

Commit 36c4c4d

Browse files
Add resistor-color-duo exercise
1 parent 1e6859a commit 36c4c4d

File tree

10 files changed

+160
-0
lines changed

10 files changed

+160
-0
lines changed

config.json

+11
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,17 @@
10071007
"math"
10081008
]
10091009
},
1010+
{
1011+
"slug": "resistor-color-duo",
1012+
"name": "Resistor Color Duo",
1013+
"uuid": "6d127442-d181-4654-8aec-36d42f09a949",
1014+
"practices": [],
1015+
"prerequisites": [
1016+
"vectors",
1017+
"numbers"
1018+
],
1019+
"difficulty": 3
1020+
},
10101021
{
10111022
"slug": "proverb",
10121023
"name": "Proverb",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
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.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- black: 0
21+
- brown: 1
22+
- red: 2
23+
- orange: 3
24+
- yellow: 4
25+
- green: 5
26+
- blue: 6
27+
- violet: 7
28+
- grey: 8
29+
- white: 9
30+
31+
From the example above:
32+
brown-green should return 15, and
33+
brown-green-violet should return 15 too, ignoring the third color.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/resistor_color_duo.clj"
8+
],
9+
"test": [
10+
"test/resistor_color_duo_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(ns resistor-color-duo)
2+
3+
(def colors ["black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"])
4+
5+
(defn- color-code [color] (.indexOf colors color))
6+
7+
(defn value
8+
"Returns the resistor value based on the given colors"
9+
[[color-1 color-2]]
10+
(+ (* 10 (color-code color-1)) (color-code color-2)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(ns resistor-color-duo-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
resistor-color-duo))
4+
{{#test_cases.value}}
5+
(deftest value_test_{{idx}}
6+
(testing {{description}}
7+
(is (= {{expected}} (resistor-color-duo/value {{input.colors}})))))
8+
{{/test_cases.value~}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
13+
description = "Brown and black"
14+
15+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
16+
description = "Blue and grey"
17+
18+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
19+
description = "Yellow and violet"
20+
21+
[b7a6cbd2-ae3c-470a-93eb-56670b305640]
22+
description = "White and red"
23+
24+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
25+
description = "Orange and orange"
26+
27+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
28+
description = "Ignore additional colors"
29+
30+
[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
31+
description = "Black and brown, one-digit"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{:aliases {:test {:extra-paths ["test"]
2+
:extra-deps {io.github.cognitect-labs/test-runner
3+
{:git/url "https://github.com/cognitect-labs/test-runner.git"
4+
:sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}}
5+
:main-opts ["-m" "cognitect.test-runner"]
6+
:exec-fn cognitect.test-runner.api/test}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject resistor-color-duo "0.1.0-SNAPSHOT"
2+
:description "resistor-color-duo exercise."
3+
:url "https://github.com/exercism/clojure/tree/main/exercises/practice/resistor-color-duo"
4+
:dependencies [[org.clojure/clojure "1.11.1"]])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns resistor-color-duo)
2+
3+
(defn value
4+
"Returns the resistor value based on the given colors"
5+
[colors]
6+
;; function body
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(ns resistor-color-duo-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
resistor-color-duo))
4+
5+
(deftest value_test_1
6+
(testing "Brown and black"
7+
(is (= 10 (resistor-color-duo/value ["brown" "black"])))))
8+
9+
(deftest value_test_2
10+
(testing "Blue and grey"
11+
(is (= 68 (resistor-color-duo/value ["blue" "grey"])))))
12+
13+
(deftest value_test_3
14+
(testing "Yellow and violet"
15+
(is (= 47 (resistor-color-duo/value ["yellow" "violet"])))))
16+
17+
(deftest value_test_4
18+
(testing "White and red"
19+
(is (= 92 (resistor-color-duo/value ["white" "red"])))))
20+
21+
(deftest value_test_5
22+
(testing "Orange and orange"
23+
(is (= 33 (resistor-color-duo/value ["orange" "orange"])))))
24+
25+
(deftest value_test_6
26+
(testing "Ignore additional colors"
27+
(is (= 51 (resistor-color-duo/value ["green" "brown" "orange"])))))
28+
29+
(deftest value_test_7
30+
(testing "Black and brown, one-digit"
31+
(is (= 1 (resistor-color-duo/value ["black" "brown"])))))

0 commit comments

Comments
 (0)