-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New Exercise]: Resistor Color Expert (#54)
* [New Exercise]: Resistor Color Expert * Update example.gd * Update test suite and example
- Loading branch information
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
exercises/practice/resistor-color-expert/.docs/instructions.md
This file contains 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,79 @@ | ||
# Instructions | ||
|
||
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 1, 4, or 5 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 trio` you decoded the first three colors. | ||
For instance: orange-orange-brown translated to the main value `330`. | ||
In this exercise you will need to add _tolerance_ to the mix. | ||
Tolerance is the maximum amount that a value can be above or below the main value. | ||
For example, if the last band is green, the maximum tolerance will be ±0.5%. | ||
|
||
The tolerance band will have one of these values: | ||
|
||
- Grey - 0.05% | ||
- Violet - 0.1% | ||
- Blue - 0.25% | ||
- Green - 0.5% | ||
- Brown - 1% | ||
- Red - 2% | ||
- Gold - 5% | ||
- Silver - 10% | ||
|
||
The four-band resistor is built up like this: | ||
|
||
| Band_1 | Band_2 | Band_3 | band_4 | | ||
| ------- | ------- | ---------- | --------- | | ||
| Value_1 | Value_2 | Multiplier | Tolerance | | ||
|
||
Meaning | ||
|
||
- orange-orange-brown-green would be 330 ohms with a ±0.5% tolerance. | ||
- orange-orange-red-grey would be 3300 ohms with ±0.05% tolerance. | ||
|
||
The difference between a four and five-band resistor is that the five-band resistor has an extra band to indicate a more precise value. | ||
|
||
| Band_1 | Band_2 | Band_3 | Band_4 | band_5 | | ||
| ------- | ------- | ------- | ---------- | --------- | | ||
| Value_1 | Value_2 | Value_3 | Multiplier | Tolerance | | ||
|
||
Meaning | ||
|
||
- orange-orange-orange-black-green would be 333 ohms with a ±0.5% tolerance. | ||
- orange-red-orange-blue-violet would be 323M ohms with a ±0.10 tolerance. | ||
|
||
There are also one band resistors. | ||
One band resistors only have the color black with a value of 0. | ||
|
||
This exercise is about translating the resistor band colors into a label: | ||
|
||
"... ohms ...%" | ||
|
||
So an input of "orange", "orange", "black", "green" should return: | ||
|
||
"33 ohms ±0.5%" | ||
|
||
When there are more than a thousand ohms, we say "kiloohms". | ||
That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams. | ||
|
||
So an input of "orange", "orange", "orange", "grey" should return: | ||
|
||
"33 kiloohms ±0.05%" | ||
|
||
When there are more than a million ohms, we say "megaohms". | ||
|
||
So an input of "orange", "orange", "blue", "red" should return: | ||
|
||
"33 megaohms ±2%" |
10 changes: 10 additions & 0 deletions
10
exercises/practice/resistor-color-expert/.docs/introduction.md
This file contains 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 @@ | ||
# Introduction | ||
|
||
If you want to build something using a Raspberry Pi, you'll probably use _resistors_. | ||
Like the previous `Resistor Color Duo` and `Resistor Color Trio` exercises, you will be translating resistor color bands to human-readable labels. | ||
|
||
- 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. |
17 changes: 17 additions & 0 deletions
17
exercises/practice/resistor-color-expert/.meta/config.json
This file contains 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,17 @@ | ||
{ | ||
"authors": ["glaxxie"], | ||
"files": { | ||
"solution": [ | ||
"resistor_color_expert.gd" | ||
], | ||
"test": [ | ||
"resistor_color_expert_test.gd" | ||
], | ||
"example": [ | ||
".meta/example.gd" | ||
] | ||
}, | ||
"blurb": "Convert color codes as used on resistors with different bands to a human-readable label.", | ||
"source": "Based on earlier resistor color exercises made by Erik Schierboom and Maud de Vries", | ||
"source_url": "https://github.com/exercism/problem-specifications/issues/1464" | ||
} |
This file contains 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,34 @@ | ||
func color_code(colors): | ||
var size = colors.size() | ||
if size == 1: return "0 ohms" | ||
|
||
var colors_code = ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"] | ||
var units = { | ||
1e9: "gigaohms", | ||
1e6: "megaohms", | ||
1e3: "kiloohms" | ||
} | ||
var tolerances = { | ||
"grey" : 0.05, | ||
"violet" : 0.1, | ||
"blue" : 0.25, | ||
"green" : 0.5, | ||
"brown" : 1, | ||
"red" : 2, | ||
"gold" : 5, | ||
"silver" : 10 | ||
} | ||
|
||
var total = 0 | ||
var magnitute = pow(10, colors_code.find(colors[-2])) | ||
|
||
for i in range(0, size - 2): | ||
var color = colors[i] | ||
total += colors_code.find(color) * pow(10, (size - i - 3)) | ||
total *= magnitute | ||
|
||
var tolerance = tolerances[colors[-1]] #work | ||
for key in units.keys(): | ||
if total >= key: | ||
return "%s %s ±%s%%" % [(total / key), units[key], tolerance] | ||
return "%s ohms ±%s%%" % [total, tolerance] |
This file contains 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 @@ | ||
# 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. | ||
|
||
[8c4f9fb6-d477-4250-bc57-b325d2be226f] | ||
description = "Orange, orange, black, and red" | ||
|
||
[d1d4a769-9210-43cc-9a14-6af6ce4c0b00] | ||
description = "Blue, grey, brown, and violet" | ||
|
||
[6af91bc3-8275-4c38-920a-185d30feb5f3] | ||
description = "Red, black, red, and green" | ||
|
||
[9c4630bf-0dda-4212-baca-2f5111530b4d] | ||
description = "Green, brown, orange, and grey" | ||
|
||
[5880ddf1-0dc6-4bd0-b9de-5626117cd2c7] | ||
description = "One black band" | ||
|
||
[a5cfda34-3c02-4bda-b183-726791fb43b2] | ||
description = "Orange, orange, yellow, black, and brown" | ||
|
||
[4f0ad96c-cdab-4c84-95dd-7074e889e001] | ||
description = "Red, green, yellow, yellow, and brown" | ||
|
||
[48c66841-208c-46a7-8992-1e84a2eda9e2] | ||
description = "Blue, grey, white, brown, and brown" | ||
|
||
[4f3aeb0c-fd9d-4cbd-b204-554e61978f73] | ||
description = "Violet, orange, red, and grey" | ||
|
||
[1ae3a17f-8bc0-449c-9831-fddfd2d91c5d] | ||
description = "Brown, red, orange, green, and blue" |
2 changes: 2 additions & 0 deletions
2
exercises/practice/resistor-color-expert/resistor_color_expert.gd
This file contains 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 @@ | ||
func color_code(colors): | ||
pass |
59 changes: 59 additions & 0 deletions
59
exercises/practice/resistor-color-expert/resistor_color_expert_test.gd
This file contains 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,59 @@ | ||
func test_orange_orange_black_and_red(solution_script): | ||
var colors = ["orange", "orange", "black", "red"] | ||
var expected = "33 ohms ±2%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_blue_grey_brown_and_violet(solution_script): | ||
var colors = ["blue", "grey", "brown", "violet"] | ||
var expected = "680 ohms ±0.1%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_red_black_red_and_green(solution_script): | ||
var colors = ["red", "black", "red", "green"] | ||
var expected = "2 kiloohms ±0.5%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_green_brown_orange_and_grey(solution_script): | ||
var colors = ["green", "brown", "orange", "grey"] | ||
var expected = "51 kiloohms ±0.05%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_one_black_band(solution_script): | ||
var colors = ["black"] | ||
var expected = "0 ohms" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_orange_orange_yellow_black_and_brown(solution_script): | ||
var colors = ["orange", "orange", "yellow", "black", "brown"] | ||
var expected = "334 ohms ±1%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_red_green_yellow_yellow_and_brown(solution_script): | ||
var colors = ["red", "green", "yellow", "yellow", "brown"] | ||
var expected = "2.54 megaohms ±1%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_blue_grey_white_red_and_brown(solution_script): | ||
var colors = ["blue", "grey", "white", "brown", "brown"] | ||
var expected = "6.89 kiloohms ±1%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_violet_orange_red_and_grey(solution_script): | ||
var colors = ["violet", "orange", "red", "grey"] | ||
var expected = "7.3 kiloohms ±0.05%" | ||
return [expected, solution_script.color_code(colors)] | ||
|
||
|
||
func test_brown_red_orange_green_and_blue(solution_script): | ||
var colors = ["brown", "red", "orange", "green", "blue"] | ||
var expected = "12.3 megaohms ±0.25%" | ||
return [expected, solution_script.color_code(colors)] | ||
|