Skip to content

Commit c24c6fb

Browse files
authored
Add resistor-color-trio (#747)
* Add `resistor-color-trio` * Apply more suggestions
1 parent 4daecbf commit c24c6fb

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@
483483
],
484484
"difficulty": 2
485485
},
486+
{
487+
"slug": "resistor-color-trio",
488+
"name": "Resistor Color Trio",
489+
"uuid": "761f7552-c192-4d46-a21c-fa30c4788307",
490+
"practices": [
491+
"lists",
492+
"pattern-matching"
493+
],
494+
"prerequisites": [
495+
"lists",
496+
"basics-2",
497+
"custom-types",
498+
"pattern-matching"
499+
],
500+
"difficulty": 3
501+
},
486502
{
487503
"slug": "space-age",
488504
"name": "Space Age",
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+
"src/ResistorColorTrio.elm"
8+
],
9+
"test": [
10+
"tests/Tests.elm"
11+
],
12+
"example": [
13+
".meta/src/ResistorColorTrio.example.elm"
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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module ResistorColorTrio exposing (Color(..), label)
2+
3+
4+
type Color
5+
= Black
6+
| Brown
7+
| Red
8+
| Orange
9+
| Yellow
10+
| Green
11+
| Blue
12+
| Violet
13+
| Grey
14+
| White
15+
16+
17+
label : List Color -> String
18+
label colors =
19+
let
20+
( magnitude, unit ) =
21+
colors |> totalResistance |> formatted
22+
in
23+
String.fromInt magnitude ++ " " ++ unit
24+
25+
26+
formatted : Int -> ( Int, String )
27+
formatted resistance =
28+
if resistance < 1000 then
29+
( resistance, "ohms" )
30+
31+
else if resistance < 1000000 then
32+
( resistance // 1000, "kiloohms" )
33+
34+
else if resistance < 1000000000 then
35+
( resistance // 1000000, "megaohms" )
36+
37+
else
38+
( resistance // 1000000000, "gigaohms" )
39+
40+
41+
totalResistance : List Color -> Int
42+
totalResistance colors =
43+
case colors of
44+
first :: second :: third :: _ ->
45+
(bandValue first * 10 + bandValue second) * 10 ^ bandValue third
46+
47+
first :: second :: [] ->
48+
bandValue first * 10 + bandValue second
49+
50+
first :: [] ->
51+
bandValue first
52+
53+
_ ->
54+
-1
55+
56+
57+
bandValue : Color -> Int
58+
bandValue color =
59+
case color of
60+
Black ->
61+
0
62+
63+
Brown ->
64+
1
65+
66+
Red ->
67+
2
68+
69+
Orange ->
70+
3
71+
72+
Yellow ->
73+
4
74+
75+
Green ->
76+
5
77+
78+
Blue ->
79+
6
80+
81+
Violet ->
82+
7
83+
84+
Grey ->
85+
8
86+
87+
White ->
88+
9
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "application",
3+
"source-directories": [
4+
"src"
5+
],
6+
"elm-version": "0.19.1",
7+
"dependencies": {
8+
"direct": {
9+
"elm/core": "1.0.5",
10+
"elm/json": "1.1.3",
11+
"elm/parser": "1.1.0",
12+
"elm/random": "1.0.0",
13+
"elm/regex": "1.0.0",
14+
"elm/time": "1.0.0",
15+
"elm/html": "1.0.0"
16+
},
17+
"indirect": {}
18+
},
19+
"test-dependencies": {
20+
"direct": {
21+
"elm-explorations/test": "2.1.0",
22+
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
23+
},
24+
"indirect": {
25+
"elm/bytes": "1.0.8",
26+
"elm/virtual-dom": "1.0.3"
27+
}
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module ResistorColorTrio exposing (Color(..), label)
2+
3+
4+
type Color
5+
= Black
6+
| Brown
7+
| Red
8+
| Orange
9+
| Yellow
10+
| Green
11+
| Blue
12+
| Violet
13+
| Grey
14+
| White
15+
16+
17+
label : List Color -> String
18+
label colors =
19+
Debug.todo "Please implement label"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module Tests exposing (tests)
2+
3+
import Expect
4+
import ResistorColorTrio exposing (Color(..), label)
5+
import Test exposing (Test, describe, skip, test)
6+
7+
8+
tests : Test
9+
tests =
10+
describe "ResistorColorTrio"
11+
[ test "Orange and orange and black" <|
12+
\() ->
13+
ResistorColorTrio.label [ Orange, Orange, Black ]
14+
|> Expect.equal "33 ohms"
15+
, skip <|
16+
test "Blue and grey and brown" <|
17+
\() ->
18+
ResistorColorTrio.label [ Blue, Grey, Brown ]
19+
|> Expect.equal "680 ohms"
20+
, skip <|
21+
test "Red and black and red" <|
22+
\() ->
23+
ResistorColorTrio.label [ Red, Black, Red ]
24+
|> Expect.equal "2 kiloohms"
25+
, skip <|
26+
test "Green and brown and orange" <|
27+
\() ->
28+
ResistorColorTrio.label [ Green, Brown, Orange ]
29+
|> Expect.equal "51 kiloohms"
30+
, skip <|
31+
test "Yellow and violet and yellow" <|
32+
\() ->
33+
ResistorColorTrio.label [ Yellow, Violet, Yellow ]
34+
|> Expect.equal "470 kiloohms"
35+
, skip <|
36+
test "Blue and violet and blue" <|
37+
\() ->
38+
ResistorColorTrio.label [ Blue, Violet, Blue ]
39+
|> Expect.equal "67 megaohms"
40+
, skip <|
41+
test "Minimum possible value" <|
42+
\() ->
43+
ResistorColorTrio.label [ Black, Black, Black ]
44+
|> Expect.equal "0 ohms"
45+
, skip <|
46+
test "Maximum possible value" <|
47+
\() ->
48+
ResistorColorTrio.label [ White, White, White ]
49+
|> Expect.equal "99 gigaohms"
50+
, skip <|
51+
test "First two colors make an invalid octal number" <|
52+
\() ->
53+
ResistorColorTrio.label [ Black, Grey, Black ]
54+
|> Expect.equal "8 ohms"
55+
, skip <|
56+
test "Ignore extra colors" <|
57+
\() ->
58+
ResistorColorTrio.label [ Blue, Green, Yellow, Orange ]
59+
|> Expect.equal "650 kiloohms"
60+
]

0 commit comments

Comments
 (0)