Skip to content
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

[resistor-color-trio] Implementation #666

Merged
merged 3 commits into from Sep 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.json
Expand Up @@ -706,6 +706,15 @@
"prerequisites": [],
"difficulty": 1,
"topics": []
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "7741c7e6-e181-4b0e-936e-d31dd390cb44",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
}
]
},
Expand Down
56 changes: 56 additions & 0 deletions exercises/practice/resistor-color-trio/.docs/instructions.md
@@ -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
@@ -0,0 +1,19 @@
{
"authors": [
"habere-et-dispertire"
],
"files": {
"solution": [
"ResistorColorTrio.rakumod"
],
"test": [
"resistor-color-trio.rakutest"
],
"example": [
".meta/solutions/ResistorColorTrio.rakumod"
]
},
"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"
}
@@ -0,0 +1,16 @@
unit module ResistorColorTrio;

sub units ( $Ω ) {
given elems gather for $Ω.flip.comb { last if $_ ne 0; .take } {
when 9 { $Ω.substr( 0, *-9 ) ~ ' gigaohms' }
when 6..8 { $Ω.substr( 0, *-6 ) ~ ' megaohms' }
when 3..5 { $Ω.substr( 0, *-3 ) ~ ' kiloohms' }
default { $Ω ~ ' ohms' }
}
}

sub create-label ( *@colors ) is export {
given <black brown red orange yellow green blue violet grey white>.antipairs.Hash{ @colors[ ^ 3 ] } {
units Int.new: [~] .[ ^ 2 ].Slip, 0 x .[ 2 ]
}
}
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/template-data.yaml
@@ -0,0 +1,33 @@
unit: module

properties:
label:
test: |-
sprintf(q:to/END/, %case<input><colors>.List.Str, %case<expected><value>, %case<expected><unit>, %case<description>.raku);
cmp-ok(
create-label(<%s>),
"eq",
"%s %s",
%s,
);
END

example: |-
sub units ( $Ω ) {
given elems gather for $Ω.flip.comb { last if $_ ne 0; .take } {
when 9 { $Ω.substr( 0, *-9 ) ~ ' gigaohms' }
when 6..8 { $Ω.substr( 0, *-6 ) ~ ' megaohms' }
when 3..5 { $Ω.substr( 0, *-3 ) ~ ' kiloohms' }
default { $Ω ~ ' ohms' }
}
}

sub create-label ( *@colors ) is export {
given <black brown red orange yellow green blue violet grey white>.antipairs.Hash{ @colors[ ^ 3 ] } {
units Int.new: [~] .[ ^ 2 ].Slip, 0 x .[ 2 ]
}
}

stub: |-
sub create-label ( *@colors ) is export {
}
40 changes: 40 additions & 0 deletions exercises/practice/resistor-color-trio/.meta/tests.toml
@@ -0,0 +1,40 @@
# 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"
@@ -0,0 +1,4 @@
unit module ResistorColorTrio;

sub create-label ( *@colors ) is export {
}
@@ -0,0 +1,76 @@
#!/usr/bin/env raku
use Test;
use lib $?FILE.IO.dirname;
use ResistorColorTrio;

cmp-ok( # begin: d6863355-15b7-40bb-abe0-bfb1a25512ed
create-label(<orange orange black>),
"eq",
"33 ohms",
"Orange and orange and black",
); # end: d6863355-15b7-40bb-abe0-bfb1a25512ed

cmp-ok( # begin: 1224a3a9-8c8e-4032-843a-5224e04647d6
create-label(<blue grey brown>),
"eq",
"680 ohms",
"Blue and grey and brown",
); # end: 1224a3a9-8c8e-4032-843a-5224e04647d6

cmp-ok( # begin: b8bda7dc-6b95-4539-abb2-2ad51d66a207
create-label(<red black red>),
"eq",
"2 kiloohms",
"Red and black and red",
); # end: b8bda7dc-6b95-4539-abb2-2ad51d66a207

cmp-ok( # begin: 5b1e74bc-d838-4eda-bbb3-eaba988e733b
create-label(<green brown orange>),
"eq",
"51 kiloohms",
"Green and brown and orange",
); # end: 5b1e74bc-d838-4eda-bbb3-eaba988e733b

cmp-ok( # begin: f5d37ef9-1919-4719-a90d-a33c5a6934c9
create-label(<yellow violet yellow>),
"eq",
"470 kiloohms",
"Yellow and violet and yellow",
); # end: f5d37ef9-1919-4719-a90d-a33c5a6934c9

cmp-ok( # begin: 5f6404a7-5bb3-4283-877d-3d39bcc33854
create-label(<blue violet blue>),
"eq",
"67 megaohms",
"Blue and violet and blue",
); # end: 5f6404a7-5bb3-4283-877d-3d39bcc33854

cmp-ok( # begin: 7d3a6ab8-e40e-46c3-98b1-91639fff2344
create-label(<black black black>),
"eq",
"0 ohms",
"Minimum possible value",
); # end: 7d3a6ab8-e40e-46c3-98b1-91639fff2344

cmp-ok( # begin: ca0aa0ac-3825-42de-9f07-dac68cc580fd
create-label(<white white white>),
"eq",
"99 gigaohms",
"Maximum possible value",
); # end: ca0aa0ac-3825-42de-9f07-dac68cc580fd

cmp-ok( # begin: 0061a76c-903a-4714-8ce2-f26ce23b0e09
create-label(<black grey black>),
"eq",
"8 ohms",
"First two colors make an invalid octal number",
); # end: 0061a76c-903a-4714-8ce2-f26ce23b0e09

cmp-ok( # begin: 30872c92-f567-4b69-a105-8455611c10c4
create-label(<blue green yellow orange>),
"eq",
"650 kiloohms",
"Ignore extra colors",
); # end: 30872c92-f567-4b69-a105-8455611c10c4

done-testing;