Skip to content

Commit 5ab3dfc

Browse files
authored
add resistor-color-trio exercise (#434)
1 parent 18bd2db commit 5ab3dfc

File tree

7 files changed

+230
-0
lines changed

7 files changed

+230
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,14 @@
934934
"practices": [],
935935
"prerequisites": [],
936936
"difficulty": 5
937+
},
938+
{
939+
"slug": "resistor-color-trio",
940+
"name": "Resistor Color Trio",
941+
"uuid": "e63a974b-1d30-4323-8059-98d131d52f6e",
942+
"practices": [],
943+
"prerequisites": [],
944+
"difficulty": 2
937945
}
938946
]
939947
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 only three 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+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
9+
- Each band acts as a digit of a number.
10+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
11+
In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands.
12+
The program will take 3 colors as input, and outputs the correct value, in ohms.
13+
The color bands are encoded as follows:
14+
15+
- Black: 0
16+
- Brown: 1
17+
- Red: 2
18+
- Orange: 3
19+
- Yellow: 4
20+
- Green: 5
21+
- Blue: 6
22+
- Violet: 7
23+
- Grey: 8
24+
- White: 9
25+
26+
In Resistor Color Duo you decoded the first two colors.
27+
For instance: orange-orange got the main value `33`.
28+
The third color stands for how many zeros need to be added to the main value.
29+
The main value plus the zeros gives us a value in ohms.
30+
For the exercise it doesn't matter what ohms really are.
31+
For example:
32+
33+
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
34+
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
35+
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
36+
37+
(If Math is your thing, you may want to think of the zeros as exponents of 10.
38+
If Math is not your thing, go with the zeros.
39+
It really is the same thing, just in plain English instead of Math lingo.)
40+
41+
This exercise is about translating the colors into a label:
42+
43+
> "... ohms"
44+
45+
So an input of `"orange", "orange", "black"` should return:
46+
47+
> "33 ohms"
48+
49+
When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
50+
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".
51+
52+
For example, an input of `"orange", "orange", "orange"` should return:
53+
54+
> "33 kiloohms"
55+
56+
[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"kmarker1101"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor-color-trio.el"
8+
],
9+
"test": [
10+
"resistor-color-trio-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
;;; resistor-color-trio.el --- Resistor Color Trio (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(setq band-values
9+
'(("black" . 0)
10+
("brown" . 1)
11+
("red" . 2)
12+
("orange" . 3)
13+
("yellow" . 4)
14+
("green" . 5)
15+
("blue" . 6)
16+
("violet" . 7)
17+
("grey" . 8)
18+
("white" . 9)))
19+
20+
21+
(defun label (colors)
22+
(let* ((color-1 (nth 0 colors))
23+
(color-2 (nth 1 colors))
24+
(color-3 (nth 2 colors))
25+
(value (+ (* (cdr (assoc color-1 band-values)) 10)
26+
(cdr (assoc color-2 band-values))))
27+
(resistance (* value (expt 10 (cdr (assoc color-3 band-values))))))
28+
(cond
29+
((< resistance 1000)
30+
(format "%d ohms" resistance))
31+
((< resistance 1000000)
32+
(format "%d kiloohms" (/ resistance 1000)))
33+
((< resistance 1000000000)
34+
(format "%d megaohms" (/ resistance 1000000)))
35+
(t
36+
(format "%d gigaohms" (/ resistance 1000000000))))))
37+
38+
39+
(provide 'resistor-color-trio)
40+
;;; resistor-color-trio.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[d6863355-15b7-40bb-abe0-bfb1a25512ed]
13+
description = "Orange and orange and black"
14+
15+
[1224a3a9-8c8e-4032-843a-5224e04647d6]
16+
description = "Blue and grey and brown"
17+
18+
[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
19+
description = "Red and black and red"
20+
21+
[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
22+
description = "Green and brown and orange"
23+
24+
[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
25+
description = "Yellow and violet and yellow"
26+
27+
[5f6404a7-5bb3-4283-877d-3d39bcc33854]
28+
description = "Blue and violet and blue"
29+
30+
[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
31+
description = "Minimum possible value"
32+
33+
[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
34+
description = "Maximum possible value"
35+
36+
[0061a76c-903a-4714-8ce2-f26ce23b0e09]
37+
description = "First two colors make an invalid octal number"
38+
39+
[30872c92-f567-4b69-a105-8455611c10c4]
40+
description = "Ignore extra colors"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
;;; resistor-color-trio-test.el --- Resistor Color Trio (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "resistor-color-trio.el")
9+
(declare-function label "resistor-color-trio.el" (colors))
10+
11+
12+
(ert-deftest orange-and-orange-and-black ()
13+
(should (string= (label '("orange" "orange" "black"))
14+
"33 ohms")))
15+
16+
(ert-deftest blue-and-grey-and-brown ()
17+
(should (string= (label '("blue" "grey" "brown"))
18+
"680 ohms")))
19+
20+
(ert-deftest red-and-black-and-red ()
21+
(should (string= (label '("red" "black" "red"))
22+
"2 kiloohms")))
23+
24+
(ert-deftest green-and-brown-and-orange ()
25+
(should (string= (label '("green" "brown" "orange"))
26+
"51 kiloohms")))
27+
28+
(ert-deftest yellow-and-violet-and-yellow ()
29+
(should (string= (label '("yellow" "violet" "yellow"))
30+
"470 kiloohms")))
31+
32+
(ert-deftest blue-and-violet-and-blue ()
33+
(should (string= (label '("blue" "violet" "blue"))
34+
"67 megaohms")))
35+
36+
(ert-deftest minimum-possible-value ()
37+
(should (string= (label '("black" "black" "black"))
38+
"0 ohms")))
39+
40+
(ert-deftest maximum-possible-value ()
41+
(should (string= (label '("white" "white" "white"))
42+
"99 gigaohms")))
43+
44+
(ert-deftest first-two-colors-make-an-invalid-octal-number ()
45+
(should (string= (label '("black" "grey" "black"))
46+
"8 ohms")))
47+
48+
(ert-deftest ignore-extra-colors ()
49+
(should (string= (label '("blue" "green" "yellow" "orange"))
50+
"650 kiloohms")))
51+
52+
(provide 'resistor-color-trio-test)
53+
;;; resistor-color-trio-test.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;;; resistor-color-trio.el --- Resistor Color Trio (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun label (colors)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(provide 'resistor-color-trio)
13+
;;; resistor-color-trio.el ends here
14+

0 commit comments

Comments
 (0)