Skip to content

Commit ad4c9ff

Browse files
authored
Add new practice exercise resistor-color-trio (#100)
1 parent 99fb53e commit ad4c9ff

File tree

10 files changed

+251
-0
lines changed

10 files changed

+251
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@
178178
"prerequisites": [],
179179
"difficulty": 5
180180
},
181+
{
182+
"slug": "resistor-color-trio",
183+
"name": "Resistor Color Trio",
184+
"uuid": "ff9d1780-bc8c-49d6-9c33-dba769d551fb",
185+
"practices": [],
186+
"prerequisites": [],
187+
"difficulty": 5
188+
},
181189
{
182190
"slug": "rest-api",
183191
"name": "REST API",
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+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor-color-trio.sql"
8+
],
9+
"test": [
10+
"resistor-color-trio_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DROP TABLE IF EXISTS dict;
2+
CREATE TABLE dict (
3+
code INTEGER,
4+
color TEXT
5+
);
6+
INSERT INTO dict (code, color)
7+
VALUES
8+
(0, 'black' ),
9+
(1, 'brown' ),
10+
(2, 'red' ),
11+
(3, 'orange' ),
12+
(4, 'yellow' ),
13+
(5, 'green' ),
14+
(6, 'blue' ),
15+
(7, 'violet' ),
16+
(8, 'grey' ),
17+
(9, 'white' );
18+
19+
UPDATE color_code
20+
SET result = (
21+
WITH value AS (
22+
SELECT (
23+
(SELECT code FROM dict WHERE color = color1) * 10
24+
+ (SELECT code FROM dict WHERE color = color2))
25+
* POW(10, (SELECT code FROM dict WHERE color = color3))
26+
AS v
27+
)
28+
SELECT
29+
CASE
30+
WHEN v >= 1e9 THEN CAST(v / 1e9 AS INTEGER) || ' gigaohms'
31+
WHEN v >= 1e6 THEN CAST(v / 1e6 AS INTEGER) || ' megaohms'
32+
WHEN v >= 1e3 THEN CAST(v / 1e3 AS INTEGER) || ' kiloohms'
33+
ELSE CAST(v AS INTEGER) || ' ohms'
34+
END
35+
FROM value
36+
)
37+
;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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"
41+
include = false
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- The color_code has specific colors and needs the result filled in.
2+
DROP TABLE IF EXISTS color_code;
3+
CREATE TABLE color_code (
4+
color1 TEXT,
5+
color2 TEXT,
6+
color3 TEXT,
7+
result TEXT
8+
);
9+
.mode csv
10+
.import ./data.csv color_code
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
color1 TEXT NOT NULL,
14+
color2 TEXT NOT NULL,
15+
color3 TEXT NOT NULL,
16+
expected TEXT NOT NULL
17+
);
18+
19+
INSERT INTO tests (uuid, description, color1, color2, color3, expected)
20+
VALUES
21+
('d6863355-15b7-40bb-abe0-bfb1a25512ed','Orange and orange and black','orange','orange','black','33 ohms'),
22+
('1224a3a9-8c8e-4032-843a-5224e04647d6','Blue and grey and brown','blue','grey','brown','680 ohms'),
23+
('b8bda7dc-6b95-4539-abb2-2ad51d66a207','Red and black and red','red','black','red','2 kiloohms'),
24+
('5b1e74bc-d838-4eda-bbb3-eaba988e733b','Green and brown and orange','green','brown','orange','51 kiloohms'),
25+
('f5d37ef9-1919-4719-a90d-a33c5a6934c9','Yellow and violet and yellow','yellow','violet','yellow','470 kiloohms'),
26+
('5f6404a7-5bb3-4283-877d-3d39bcc33854','Blue and violet and blue','blue','violet','blue','67 megaohms'),
27+
('7d3a6ab8-e40e-46c3-98b1-91639fff2344','Minimum possible value','black','black','black','0 ohms'),
28+
('ca0aa0ac-3825-42de-9f07-dac68cc580fd','Maximum possible value','white','white','white','99 gigaohms'),
29+
('0061a76c-903a-4714-8ce2-f26ce23b0e09','First two colors make an invalid octal number','black','grey','black','8 ohms');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"orange","orange","black",""
2+
"blue","grey","brown",""
3+
"red","black","red",""
4+
"green","brown","orange",""
5+
"yellow","violet","yellow",""
6+
"blue","violet","blue",""
7+
"black","black","black",""
8+
"white","white","white",""
9+
"black","grey","black",""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Schema: CREATE TABLE IF NOT EXISTS color_code ( color1 TEXT, color2 TEXT, color3 TEXT, result TEXT );
2+
-- Task: update the color_code table and set the result based on the colors.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Create database:
2+
.read ./create_fixture.sql
3+
4+
-- Read user student solution and save any output as markdown in user_output.md:
5+
.mode markdown
6+
.output user_output.md
7+
.read ./resistor-color-trio.sql
8+
.output
9+
10+
-- Create a clean testing environment:
11+
.read ./create_test_table.sql
12+
13+
-- Comparison of user input and the tests updates the status for each test:
14+
UPDATE tests
15+
SET status = 'pass'
16+
FROM (SELECT color1, color2, color3, result FROM color_code) AS actual
17+
WHERE (actual.color1, actual.color2, actual.color3, actual.result) = (tests.color1, tests.color2, tests.color3, tests.expected);
18+
19+
-- Update message for failed tests to give helpful information:
20+
UPDATE tests
21+
SET message = (
22+
'Result for "'
23+
|| tests.color1 || ',' || tests.color2 || ',' || tests.color3
24+
|| '"'
25+
|| ' is <' || COALESCE(actual.result, 'NULL')
26+
|| '> but should be <' || tests.expected || '>'
27+
)
28+
FROM (SELECT color1, color2, color3, result FROM color_code) AS actual
29+
WHERE (actual.color1, actual.color2, actual.color3) = (tests.color1, tests.color2, tests.color3) AND tests.status = 'fail';
30+
31+
-- Save results to ./output.json (needed by the online test-runner)
32+
.mode json
33+
.once './output.json'
34+
SELECT description, status, message, output, test_code, task_id
35+
FROM tests;
36+
37+
-- Display test results in readable form for the student:
38+
.mode table
39+
SELECT description, status, message
40+
FROM tests;

0 commit comments

Comments
 (0)