Skip to content

Commit df41143

Browse files
Add resistor-color exercise (#758)
1 parent 759165c commit df41143

File tree

10 files changed

+151
-0
lines changed

10 files changed

+151
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,17 @@
701701
"conditionals"
702702
]
703703
},
704+
{
705+
"slug": "resistor-color",
706+
"name": "Resistor Color",
707+
"uuid": "a5db6df9-88e3-4188-a45e-f25ad6565083",
708+
"practices": [],
709+
"prerequisites": [
710+
"strings",
711+
"vectors"
712+
],
713+
"difficulty": 2
714+
},
704715
{
705716
"slug": "word-count",
706717
"name": "Word Count",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
14+
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
15+
16+
These colors are encoded as follows:
17+
18+
- black: 0
19+
- brown: 1
20+
- red: 2
21+
- orange: 3
22+
- yellow: 4
23+
- green: 5
24+
- blue: 6
25+
- violet: 7
26+
- grey: 8
27+
- white: 9
28+
29+
The goal of this exercise is to create a way:
30+
31+
- to look up the numerical value associated with a particular color band
32+
- to list the different band colors
33+
34+
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array:
35+
Better Be Right Or Your Great Big Values Go Wrong.
36+
37+
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code].
38+
39+
[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
Lines changed: 19 additions & 0 deletions
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.clj"
8+
],
9+
"test": [
10+
"test/resistor_color_test.clj"
11+
],
12+
"example": [
13+
".meta/example.clj"
14+
]
15+
},
16+
"blurb": "Convert a resistor band's color to its numeric representation.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns resistor-color)
2+
3+
(def colors ["black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"])
4+
5+
(defn color-code [color] (.indexOf colors color))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns resistor-color-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
resistor-color))
4+
{{#test_cases.colors}}
5+
(deftest colors_test_{{idx}}
6+
(testing {{string description}}
7+
(is (= {{expected}}
8+
resistor-color/colors))))
9+
{{/test_cases.colors~}}
10+
{{#test_cases.colorCode}}
11+
(deftest color-code_test_{{idx}}
12+
(testing {{string description}}
13+
(is (= {{expected}} (resistor-color/color-code {{string input.color}})))))
14+
{{/test_cases.colorCode~}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
[49eb31c5-10a8-4180-9f7f-fea632ab87ef]
13+
description = "Color codes -> Black"
14+
15+
[0a4df94b-92da-4579-a907-65040ce0b3fc]
16+
description = "Color codes -> White"
17+
18+
[5f81608d-f36f-4190-8084-f45116b6f380]
19+
description = "Color codes -> Orange"
20+
21+
[581d68fa-f968-4be2-9f9d-880f2fb73cf7]
22+
description = "Colors"
Lines changed: 6 additions & 0 deletions
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}}}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(defproject resistor-color "0.1.0-SNAPSHOT"
2+
:description "resistor-color exercise."
3+
:url "https://github.com/exercism/clojure/tree/main/exercises/practice/resistor-color"
4+
:dependencies [[org.clojure/clojure "1.11.1"]])
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(ns resistor-color)
2+
3+
(def colors
4+
;; definition value
5+
)
6+
7+
(defn color-code
8+
"Returns the numerical value associated with the given color"
9+
[color]
10+
;; function body
11+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(ns resistor-color-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
resistor-color))
4+
5+
(deftest colors_test_1
6+
(testing "Colors"
7+
(is (= ["black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"]
8+
resistor-color/colors))))
9+
10+
(deftest color-code_test_1
11+
(testing "Color codes - Black"
12+
(is (= 0 (resistor-color/color-code "black")))))
13+
14+
(deftest color-code_test_2
15+
(testing "Color codes - White"
16+
(is (= 9 (resistor-color/color-code "white")))))
17+
18+
(deftest color-code_test_3
19+
(testing "Color codes - Orange"
20+
(is (= 3 (resistor-color/color-code "orange")))))

0 commit comments

Comments
 (0)