Skip to content

Commit d5f9fa3

Browse files
authored
Add resistor-color-trio (#370)
1 parent 7aa330d commit d5f9fa3

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,14 @@
463463
"prerequisites": [],
464464
"difficulty": 2
465465
},
466+
{
467+
"slug": "resistor-color-trio",
468+
"name": "Resistor Color Trio",
469+
"uuid": "643e96bc-d0b2-426b-98ef-9f12d1f50b7a",
470+
"practices": [],
471+
"prerequisites": [],
472+
"difficulty": 2
473+
},
466474
{
467475
"slug": "robot-simulator",
468476
"name": "Robot Simulator",
Lines changed: 56 additions & 0 deletions
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor-color-trio.coffee"
8+
],
9+
"test": [
10+
"resistor-color-trio.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class ResistorColorTrio
2+
@label: ([tens, ones, exp]) ->
3+
value = (@colorBands.indexOf(ones) + 10 * @colorBands.indexOf(tens)) * 10 ** @colorBands.indexOf(exp)
4+
5+
if value < 1e3
6+
"#{value} ohms"
7+
else if value < 1e6
8+
"#{value / 1e3} kiloohms"
9+
else if value < 1e9
10+
"#{value / 1e6} megaohms"
11+
else
12+
"#{value / 1e9} gigaohms"
13+
14+
@colorBands: [
15+
"black"
16+
"brown"
17+
"red"
18+
"orange"
19+
"yellow"
20+
"green"
21+
"blue"
22+
"violet"
23+
"grey"
24+
"white"
25+
]
26+
27+
module.exports = ResistorColorTrio
Lines changed: 40 additions & 0 deletions
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"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ResistorColorTrio
2+
@label: (colors) ->
3+
4+
module.exports = ResistorColorTrio
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
ResistorColorTrio = require './resistor-color-trio'
2+
3+
describe 'ResistorColorTrio', ->
4+
it 'Orange and orange and black', ->
5+
results = ResistorColorTrio.label ['orange', 'orange', 'black']
6+
expect(results).toEqual '33 ohms'
7+
8+
xit 'Blue and grey and brown', ->
9+
results = ResistorColorTrio.label ['blue', 'grey', 'brown']
10+
expect(results).toEqual '680 ohms'
11+
12+
xit 'Red and black and red', ->
13+
results = ResistorColorTrio.label ['red', 'black', 'red']
14+
expect(results).toEqual '2 kiloohms'
15+
16+
xit 'Green and brown and orange', ->
17+
results = ResistorColorTrio.label ['green', 'brown', 'orange']
18+
expect(results).toEqual '51 kiloohms'
19+
20+
xit 'Yellow and violet and yellow', ->
21+
results = ResistorColorTrio.label ['yellow', 'violet', 'yellow']
22+
expect(results).toEqual '470 kiloohms'
23+
24+
xit 'Blue and violet and blue', ->
25+
results = ResistorColorTrio.label ['blue', 'violet', 'blue']
26+
expect(results).toEqual '67 megaohms'
27+
28+
xit 'Minimum possible value', ->
29+
results = ResistorColorTrio.label ['black', 'black', 'black']
30+
expect(results).toEqual '0 ohms'
31+
32+
xit 'Maximum possible value', ->
33+
results = ResistorColorTrio.label ['white', 'white', 'white']
34+
expect(results).toEqual '99 gigaohms'
35+
36+
xit 'First two colors make an invalid octal number', ->
37+
results = ResistorColorTrio.label ['black', 'grey', 'black']
38+
expect(results).toEqual '8 ohms'
39+
40+
xit 'Ignore extra colors', ->
41+
results = ResistorColorTrio.label ['blue', 'green', 'yellow', 'orange']
42+
expect(results).toEqual '650 kiloohms'

0 commit comments

Comments
 (0)