Skip to content

Commit 8993771

Browse files
authored
Exercise Resistor Color Duo from PR #37 (author gvrooyen) (#145)
The 11 exercises in PR #37 have been stuck in limbo since last year. They were mostly complete except for the description tags and some minor changes to match the track convention. This PR just includes the update work.
1 parent a5797b2 commit 8993771

File tree

7 files changed

+226
-6
lines changed

7 files changed

+226
-6
lines changed

config.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@
103103
"difficulty": 2
104104
},
105105
{
106-
"slug": "darts",
107-
"name": "Darts",
108-
"uuid": "43e5dc65-efd0-4e46-8e58-c3473d56a0fc",
106+
"slug": "dnd-character",
107+
"name": "D&D Character",
108+
"uuid": "64e89ff5-9517-430b-a747-ad85998651c0",
109109
"practices": [],
110110
"prerequisites": [],
111111
"difficulty": 2
112112
},
113113
{
114-
"slug": "dnd-character",
115-
"name": "D&D Character",
116-
"uuid": "64e89ff5-9517-430b-a747-ad85998651c0",
114+
"slug": "darts",
115+
"name": "Darts",
116+
"uuid": "43e5dc65-efd0-4e46-8e58-c3473d56a0fc",
117117
"practices": [],
118118
"prerequisites": [],
119119
"difficulty": 2
@@ -270,6 +270,14 @@
270270
"prerequisites": [],
271271
"difficulty": 2
272272
},
273+
{
274+
"slug": "resistor-color-duo",
275+
"name": "Resistor Color Duo",
276+
"uuid": "d056bbb5-172a-485f-b0cf-e4fda0d30796",
277+
"practices": [],
278+
"prerequisites": [],
279+
"difficulty": 2
280+
},
273281
{
274282
"slug": "robot-name",
275283
"name": "Robot Name",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
15+
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- black: 0
21+
- brown: 1
22+
- red: 2
23+
- orange: 3
24+
- yellow: 4
25+
- green: 5
26+
- blue: 6
27+
- violet: 7
28+
- grey: 8
29+
- white: 9
30+
31+
From the example above:
32+
brown-green should return 15, and
33+
brown-green-violet should return 15 too, ignoring the third color.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"gvrooyen"
4+
],
5+
"contributors": [
6+
"rmonnet"
7+
],
8+
"files": {
9+
"solution": [
10+
"resistor_color_duo.odin"
11+
],
12+
"test": [
13+
"resistor_color_duo_test.odin"
14+
],
15+
"example": [
16+
".meta/example.odin"
17+
]
18+
},
19+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
20+
"source": "Maud de Vries, Erik Schierboom",
21+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package resistor_color_duo
2+
3+
Color :: enum {
4+
Black,
5+
Brown,
6+
Red,
7+
Orange,
8+
Yellow,
9+
Green,
10+
Blue,
11+
Violet,
12+
Grey,
13+
White,
14+
}
15+
16+
Error :: enum {
17+
None,
18+
TooFewColors,
19+
Unimplemented,
20+
}
21+
22+
value :: proc(colors: []Color) -> (int, Error) {
23+
if len(colors) < 2 {
24+
return 0, .TooFewColors
25+
} else {
26+
return int(colors[0]) * 10 + int(colors[1]), .None
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
13+
description = "Brown and black"
14+
15+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
16+
description = "Blue and grey"
17+
18+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
19+
description = "Yellow and violet"
20+
21+
[b7a6cbd2-ae3c-470a-93eb-56670b305640]
22+
description = "White and red"
23+
24+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
25+
description = "Orange and orange"
26+
27+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
28+
description = "Ignore additional colors"
29+
30+
[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
31+
description = "Black and brown, one-digit"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package resistor_color_duo
2+
3+
Color :: enum {
4+
Black,
5+
Brown,
6+
Red,
7+
Orange,
8+
Yellow,
9+
Green,
10+
Blue,
11+
Violet,
12+
Grey,
13+
White,
14+
}
15+
16+
Error :: enum {
17+
None,
18+
TooFewColors,
19+
Unimplemented,
20+
}
21+
22+
value :: proc(colors: []Color) -> (int, Error) {
23+
// Implement the procedure.
24+
return 0, .Unimplemented
25+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package resistor_color_duo
2+
3+
import "core:testing"
4+
5+
@(test)
6+
/// description = Brown and black
7+
test_brown_and_black :: proc(t: ^testing.T) {
8+
c, e := value({.Brown, .Black})
9+
testing.expect_value(t, c, 10)
10+
testing.expect_value(t, e, Error.None)
11+
}
12+
13+
@(test)
14+
/// description = Blue and grey
15+
test_blue_and_grey :: proc(t: ^testing.T) {
16+
c, e := value({.Blue, .Grey})
17+
testing.expect_value(t, c, 68)
18+
testing.expect_value(t, e, Error.None)
19+
}
20+
21+
@(test)
22+
/// description = Yellow and violet
23+
test_yellow_and_violet :: proc(t: ^testing.T) {
24+
c, e := value({.Yellow, .Violet})
25+
testing.expect_value(t, c, 47)
26+
testing.expect_value(t, e, Error.None)
27+
}
28+
29+
@(test)
30+
/// description = White and red
31+
test_white_and_red :: proc(t: ^testing.T) {
32+
c, e := value({.White, .Red})
33+
testing.expect_value(t, c, 92)
34+
testing.expect_value(t, e, Error.None)
35+
}
36+
37+
@(test)
38+
/// description = Orange and orange
39+
test_orange_and_orange :: proc(t: ^testing.T) {
40+
c, e := value({.Orange, .Orange})
41+
testing.expect_value(t, c, 33)
42+
testing.expect_value(t, e, Error.None)
43+
}
44+
45+
@(test)
46+
/// description = Ignore additional colors
47+
test_ignore_additional_colors :: proc(t: ^testing.T) {
48+
c, e := value({.Green, .Brown, .Orange})
49+
testing.expect_value(t, c, 51)
50+
testing.expect_value(t, e, Error.None)
51+
}
52+
53+
@(test)
54+
/// description = Black and brown, one-digit
55+
test_black_and_brown_one_digit :: proc(t: ^testing.T) {
56+
c, e := value({.Black, .Brown})
57+
testing.expect_value(t, c, 1)
58+
testing.expect_value(t, e, Error.None)
59+
}
60+
61+
@(test)
62+
/// description = One color is an error
63+
test_one_color_is_an_error :: proc(t: ^testing.T) {
64+
_, e := value({.Violet})
65+
testing.expect_value(t, e, Error.TooFewColors)
66+
}
67+
68+
@(test)
69+
/// description = Empty slice is an error
70+
test_empty_slice_is_an_error :: proc(t: ^testing.T) {
71+
_, e := value({})
72+
testing.expect_value(t, e, Error.TooFewColors)
73+
}

0 commit comments

Comments
 (0)