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
57 changes: 33 additions & 24 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,30 +197,6 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "c15ce044-1a62-45eb-86a2-079554cbd2c2",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-expert",
"name": "Resistor Color Expert",
"uuid": "af806fda-62fa-4eaa-9b38-a1304a4aed61",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "bf63aff6-54bf-484a-b00d-9a038db249aa",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "reverse-string",
"name": "Reverse String",
Expand Down Expand Up @@ -269,6 +245,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "c15ce044-1a62-45eb-86a2-079554cbd2c2",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
Expand Down Expand Up @@ -342,6 +326,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
"uuid": "bf63aff6-54bf-484a-b00d-9a038db249aa",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "saddle-points",
"name": "Saddle Points",
Expand Down Expand Up @@ -446,6 +438,23 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "resistor-color-expert",
"name": "Resistor Color Expert",
"uuid": "af806fda-62fa-4eaa-9b38-a1304a4aed61",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"status": "deprecated"
},
{
"slug": "resistor-color-tolerance",
"name": "Resistor Color Tolerance",
"uuid": "265eb5ac-529d-4252-a203-3b9a4bbb7ef8",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "save-the-cow",
"name": "Save the Cow",
Expand Down
85 changes: 85 additions & 0 deletions exercises/practice/resistor-color-tolerance/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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 output the correct value in ohms.
The color bands are encoded as follows:

| Color | Value |
| ------ | ----- |
| Black | 0 |
| Brown | 1 |
| Red | 2 |
| Orange | 3 |
| Yellow | 4 |
| Green | 5 |
| Blue | 6 |
| Violet | 7 |
| Gray | 8 |
| White | 9 |

In [`Resistor Color Trio`][resistor-color-trio-exercise] you decoded the first three color bands.
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:

| Color | Tolerance |
| ------ | --------: |
| Grey | ±0.05% |
| Violet | ±0.10% |
| Blue | ±0.25% |
| Green | ±0.50% |
| Brown | ±1.00% |
| Red | ±2.00% |
| Gold | ±5.00% |
| Silver | ±10.00% |

The four-band resistor is built up like this:

| Band_1 | Band_2 | Band_3 | Band_4 |
| ------- | ------- | ---------- | --------- |
| Value_1 | Value_2 | Multiplier | Tolerance |

Examples:

- orange-orange-brown-green would be `330` ohms with a `±0.5%` tolerance.
- orange-orange-red-grey would be `3300` ohms with a `±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 |

Examples:

- orange-orange-orange-black-green would be `333` ohms with a `±0.5%` tolerance.
- orange-red-orange-blue-violet would be `323 million` ohms with a `±0.1%` tolerance.

There are also one band resistors.
One band resistors only have the color black with a value of 0.

Your program should translate an input `list` of resistor band colors into a label:

"... ohms ±...%"

So an input `list` of `["orange", "orange", "black", "green"]` should return:

"33 ohms ±0.5%"

When there are thousands of ohms, we say "kiloohms".
That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.

So an input `list` of `["orange", "orange", "orange", "grey"]` should return:

"33 kiloohms ±0.05%"

When there are millions of ohms, we say "megaohms".

So an input `list` of `["orange", "orange", "blue", "red"]` should return:

"33 megaohms ±2%"

[resistor-color-trio-exercise]: https://exercism.org/tracks/python/exercises/resistor-color-trio
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color-tolerance/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"IsaacG"
],
"files": {
"solution": [
"resistor_color_tolerance.gd"
],
"test": [
"resistor_color_tolerance_test.gd",
"run_tests",
"lib/test_runner.gd",
"project.godot"
],
"example": [
".meta/example.gd"
]
},
"blurb": "Convert color codes as used on resistors with different bands to a human-readable label.",
"source": "@meatball133",
"source_url": "https://github.com/exercism/python/tree/main/exercises/practice/resistor-color-expert"
}
38 changes: 38 additions & 0 deletions exercises/practice/resistor-color-tolerance/.meta/example.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
func label_decoder(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%%" % [format_number(total / key), units[key], tolerance]
return "%s ohms ±%s%%" % [format_number(total), tolerance]

func format_number(n):
return str(int(n)) if n == int(n) else str(n)

11 changes: 11 additions & 0 deletions exercises/practice/resistor-color-tolerance/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% for case in cases %}
func test_{{ case.func_name }}(solution_script):
var colors = {{ case.input.colors | togdscript }}
{%- if case.expected.tolerance %}
var expected = "{{ case.expected.value }} {{ case.expected.unit }} {{ case.expected.tolerance }}"
{%- else %}
var expected = "{{ case.expected.value }} {{ case.expected.unit }}"
{%- endif %}
return [solution_script.{{ case.property | camel_to_snake }}(colors), expected]

{% endfor %}
51 changes: 51 additions & 0 deletions exercises/practice/resistor-color-tolerance/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.

[fb677832-9a1b-4493-a5b0-786db5a8479c]
description = "Orange, orange, black and red"

[8bdeb354-b8cf-47c1-96e4-84dceda90f8c]
description = "Blue, gray, brown and violet"

[b4ef8864-09fc-443b-b4fb-82835c081637]
description = "Red, black, red and green"

[c001757b-d388-4f42-9987-23e42bc22d68]
description = "Green, brownm orange and grey"
include = false

[a182cca5-4f3c-49f2-8d46-709d11624353]
description = "Green, brownm orange and grey"
reimplements = "c001757b-d388-4f42-9987-23e42bc22d68"

[1cb18e15-1fc5-4375-8a24-ca6eee130a27]
description = "Only one band"

[7e6841b9-0d11-43fb-85ad-be50e49b0656]
description = "Orange, orange, yellow, black and brown"

[00019463-44d1-47f5-b59f-f3e7defc03d9]
description = "Red, green, yellow, yellow and brown"

[7998837e-fdfa-48c6-9191-0ab27c4777cc]
description = "Blue, gray, white, brown and brown"

[51554e9f-c21d-4a73-a54c-1d9a973eaed0]
description = "Violet, orange, red and gray"

[68aa7ba3-2334-411a-b3a4-7c47fe20fd99]
description = "Brown, red, orange, green and blue"

[f6069edc-099b-429b-b2a9-bbe2537d2fae]
description = "Brown, black, brown, yellow and violet"

[8fce56df-326e-45fe-9265-b69c334b9023]
description = "Brown, black, red and red"
Loading
Loading