-
-
Notifications
You must be signed in to change notification settings - Fork 14
Add new practical exercise resistor-color-trio #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a8e184
* add pratical exercise: resistor-color-trio
jimmytty 574020e
* add pratical exercise: resistor-color-trio
jimmytty 0ee00df
* removing fourth column
jimmytty c58bcea
* replacing function CONCAT_WS with CONCAT
jimmytty 8c9d593
* removing function CONCAT
jimmytty 26fdb30
Update exercises/practice/resistor-color-trio/.meta/tests.toml
jimmytty fa4e61c
Update exercises/practice/resistor-color-trio/create_test_table.sql
jimmytty e8a7720
* setting difficulty to 5 (medium) and sorting
jimmytty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
exercises/practice/resistor-color-trio/.docs/instructions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
29
exercises/practice/resistor-color-trio/create_test_table.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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","" |
2 changes: 2 additions & 0 deletions
2
exercises/practice/resistor-color-trio/resistor-color-trio.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
40 changes: 40 additions & 0 deletions
40
exercises/practice/resistor-color-trio/resistor-color-trio_test.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.