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

[New Exercise]: Resistor Color Expert #54

Merged
merged 3 commits into from
Mar 5, 2024
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 @@ -59,6 +59,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-expert",
"name": "Resistor Color Expert",
"uuid": "af806fda-62fa-4eaa-9b38-a1304a4aed61",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"uuid": "c3b37c54-cc4e-49a5-a869-cd7700b0e448",
"slug": "leap",
Expand Down
79 changes: 79 additions & 0 deletions exercises/practice/resistor-color-expert/.docs/instructions.md
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 exercises/practice/resistor-color-expert/.docs/introduction.md
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 exercises/practice/resistor-color-expert/.meta/config.json
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"
}
34 changes: 34 additions & 0 deletions exercises/practice/resistor-color-expert/.meta/example.gd
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]
40 changes: 40 additions & 0 deletions exercises/practice/resistor-color-expert/.meta/tests.toml
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func color_code(colors):
pass
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)]