Skip to content

Commit f2687a1

Browse files
authored
Add resistor-color-trio exercise (#53)
* Add resistor-color-trio exercise * Fix spacing Fix spacing so methods are 2 empty lines apart. * Update space * Update example.gd Fix another spacing issue
1 parent bcad10e commit f2687a1

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
"prerequisites": [],
5252
"difficulty": 2
5353
},
54+
{
55+
"slug": "resistor-color-trio",
56+
"name": "Resistor Color Trio",
57+
"uuid": "bf63aff6-54bf-484a-b00d-9a038db249aa",
58+
"practices": [],
59+
"prerequisites": [],
60+
"difficulty": 2
61+
},
5462
{
5563
"uuid": "c3b37c54-cc4e-49a5-a869-cd7700b0e448",
5664
"slug": "leap",
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+
"glaxxie"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor_color_trio.gd"
8+
],
9+
"test": [
10+
"resistor_color_trio_test.gd"
11+
],
12+
"example": [
13+
".meta/example.gd"
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,18 @@
1+
func color_code(colors):
2+
var colors_code = ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]
3+
4+
var units = {
5+
1e9: "gigaohms",
6+
1e6: "megaohms",
7+
1e3: "kiloohms"
8+
}
9+
10+
var base_value = colors_code.find(colors[0]) * 10 + colors_code.find(colors[1])
11+
var magnitude = 10 ** colors_code.find(colors[2])
12+
var total = base_value * magnitude
13+
14+
for key in units.keys():
15+
if total >= key:
16+
return str(total / key) + " " + units[key]
17+
18+
return str(total) + " ohms"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
func color_code(colors):
2+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
func test_orange_and_orange_and_black(solution_script):
2+
var colors = ["orange", "orange", "black"]
3+
var expected = "33 ohms"
4+
return [expected, solution_script.color_code(colors)]
5+
6+
7+
func test_blue_and_grey_and_brown(solution_script):
8+
var colors = ["blue", "grey", "brown"]
9+
var expected = "680 ohms"
10+
return [expected, solution_script.color_code(colors)]
11+
12+
13+
func test_red_and_black_and_red(solution_script):
14+
var colors = ["red", "black", "red"]
15+
var expected = "2 kiloohms"
16+
return [expected, solution_script.color_code(colors)]
17+
18+
19+
func test_green_and_brown_and_orange(solution_script):
20+
var colors = ["green", "brown", "orange"]
21+
var expected = "51 kiloohms"
22+
return [expected, solution_script.color_code(colors)]
23+
24+
25+
func test_yellow_and_violet_and_yellow(solution_script):
26+
var colors = ["yellow", "violet", "yellow"]
27+
var expected = "470 kiloohms"
28+
return [expected, solution_script.color_code(colors)]
29+
30+
31+
func test_blue_and_violet_and_blue(solution_script):
32+
var colors = ["blue", "violet", "blue"]
33+
var expected = "67 megaohms"
34+
return [expected, solution_script.color_code(colors)]
35+
36+
37+
func test_minimum_possible_value(solution_script):
38+
var colors = ["black", "black", "black"]
39+
var expected = "0 ohms"
40+
return [expected, solution_script.color_code(colors)]
41+
42+
43+
func test_maximum_possible_value(solution_script):
44+
var colors = ["white", "white", "white"]
45+
var expected = "99 gigaohms"
46+
return [expected, solution_script.color_code(colors)]
47+
48+
49+
func test_first_two_colors_make_an_invalid_octal_number(solution_script):
50+
var colors = ["black", "grey", "black"]
51+
var expected = "8 ohms"
52+
return [expected, solution_script.color_code(colors)]
53+
54+
55+
func test_ignore_extra_colors(solution_script):
56+
var colors = ["blue", "green", "yellow", "orange"]
57+
var expected = "650 kiloohms"
58+
return [expected, solution_script.color_code(colors)]
59+

0 commit comments

Comments
 (0)