Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "ff9d1780-bc8c-49d6-9c33-dba769d551fb",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "rest-api",
"name": "REST API",
Expand Down
56 changes: 56 additions & 0 deletions exercises/practice/resistor-color-trio/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know only three things about them:

- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
- Each band acts as a digit of a number.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 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.
The program will take 3 colors as input, and outputs the correct value, in ohms.
The color bands are encoded as follows:

- black: 0
- brown: 1
- red: 2
- orange: 3
- yellow: 4
- green: 5
- blue: 6
- violet: 7
- grey: 8
- white: 9

In Resistor Color Duo you decoded the first two colors.
For instance: orange-orange got the main value `33`.
The third color stands for how many zeros need to be added to the main value.
The main value plus the zeros gives us a value in ohms.
For the exercise it doesn't matter what ohms really are.
For example:

- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.

(If Math is your thing, you may want to think of the zeros as exponents of 10.
If Math is not your thing, go with the zeros.
It really is the same thing, just in plain English instead of Math lingo.)

This exercise is about translating the colors into a label:

> "... ohms"

So an input of `"orange", "orange", "black"` should return:

> "33 ohms"

When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".

For example, an input of `"orange", "orange", "orange"` should return:

> "33 kiloohms"

[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jimmytty"
],
"files": {
"solution": [
"resistor-color-trio.sql"
],
"test": [
"resistor-color-trio_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
}
37 changes: 37 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
DROP TABLE IF EXISTS dict;
CREATE TABLE dict (
code INTEGER,
color TEXT
);
INSERT INTO dict (code, color)
VALUES
(0, 'black' ),
(1, 'brown' ),
(2, 'red' ),
(3, 'orange' ),
(4, 'yellow' ),
(5, 'green' ),
(6, 'blue' ),
(7, 'violet' ),
(8, 'grey' ),
(9, 'white' );

UPDATE color_code
SET result = (
WITH value AS (
SELECT (
(SELECT code FROM dict WHERE color = color1) * 10
+ (SELECT code FROM dict WHERE color = color2))
* POW(10, (SELECT code FROM dict WHERE color = color3))
AS v
)
SELECT
CASE
WHEN v >= 1e9 THEN CAST(v / 1e9 AS INTEGER) || ' gigaohms'
WHEN v >= 1e6 THEN CAST(v / 1e6 AS INTEGER) || ' megaohms'
WHEN v >= 1e3 THEN CAST(v / 1e3 AS INTEGER) || ' kiloohms'
ELSE CAST(v AS INTEGER) || ' ohms'
END
FROM value
)
;
41 changes: 41 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[d6863355-15b7-40bb-abe0-bfb1a25512ed]
description = "Orange and orange and black"

[1224a3a9-8c8e-4032-843a-5224e04647d6]
description = "Blue and grey and brown"

[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
description = "Red and black and red"

[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
description = "Green and brown and orange"

[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
description = "Yellow and violet and yellow"

[5f6404a7-5bb3-4283-877d-3d39bcc33854]
description = "Blue and violet and blue"

[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
description = "Minimum possible value"

[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
description = "Maximum possible value"

[0061a76c-903a-4714-8ce2-f26ce23b0e09]
description = "First two colors make an invalid octal number"

[30872c92-f567-4b69-a105-8455611c10c4]
description = "Ignore extra colors"
include = false
10 changes: 10 additions & 0 deletions exercises/practice/resistor-color-trio/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- The color_code has specific colors and needs the result filled in.
DROP TABLE IF EXISTS color_code;
CREATE TABLE color_code (
color1 TEXT,
color2 TEXT,
color3 TEXT,
result TEXT
);
.mode csv
.import ./data.csv color_code
29 changes: 29 additions & 0 deletions exercises/practice/resistor-color-trio/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
color1 TEXT NOT NULL,
color2 TEXT NOT NULL,
color3 TEXT NOT NULL,
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, color1, color2, color3, expected)
VALUES
('d6863355-15b7-40bb-abe0-bfb1a25512ed','Orange and orange and black','orange','orange','black','33 ohms'),
('1224a3a9-8c8e-4032-843a-5224e04647d6','Blue and grey and brown','blue','grey','brown','680 ohms'),
('b8bda7dc-6b95-4539-abb2-2ad51d66a207','Red and black and red','red','black','red','2 kiloohms'),
('5b1e74bc-d838-4eda-bbb3-eaba988e733b','Green and brown and orange','green','brown','orange','51 kiloohms'),
('f5d37ef9-1919-4719-a90d-a33c5a6934c9','Yellow and violet and yellow','yellow','violet','yellow','470 kiloohms'),
('5f6404a7-5bb3-4283-877d-3d39bcc33854','Blue and violet and blue','blue','violet','blue','67 megaohms'),
('7d3a6ab8-e40e-46c3-98b1-91639fff2344','Minimum possible value','black','black','black','0 ohms'),
('ca0aa0ac-3825-42de-9f07-dac68cc580fd','Maximum possible value','white','white','white','99 gigaohms'),
('0061a76c-903a-4714-8ce2-f26ce23b0e09','First two colors make an invalid octal number','black','grey','black','8 ohms');
9 changes: 9 additions & 0 deletions exercises/practice/resistor-color-trio/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"orange","orange","black",""
"blue","grey","brown",""
"red","black","red",""
"green","brown","orange",""
"yellow","violet","yellow",""
"blue","violet","blue",""
"black","black","black",""
"white","white","white",""
"black","grey","black",""
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Schema: CREATE TABLE IF NOT EXISTS color_code ( color1 TEXT, color2 TEXT, color3 TEXT, result TEXT );
-- Task: update the color_code table and set the result based on the colors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./resistor-color-trio.sql
.output

-- Create a clean testing environment:
.read ./create_test_table.sql

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT color1, color2, color3, result FROM color_code) AS actual
WHERE (actual.color1, actual.color2, actual.color3, actual.result) = (tests.color1, tests.color2, tests.color3, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| tests.color1 || ',' || tests.color2 || ',' || tests.color3
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT color1, color2, color3, result FROM color_code) AS actual
WHERE (actual.color1, actual.color2, actual.color3) = (tests.color1, tests.color2, tests.color3) AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT description, status, message, output, test_code, task_id
FROM tests;

-- Display test results in readable form for the student:
.mode table
SELECT description, status, message
FROM tests;