Added resistor_color_trio_test
This commit is contained in:
14
config.json
14
config.json
@@ -682,6 +682,20 @@
|
||||
],
|
||||
"difficulty": 2
|
||||
},
|
||||
{
|
||||
"slug": "resistor-color-trio",
|
||||
"name": "Resistor Color Trio",
|
||||
"uuid": "089f06a6-0759-479c-8c00-d699525a1e22",
|
||||
"practices": ["list-methods"],
|
||||
"prerequisites": [
|
||||
"basics",
|
||||
"bools",
|
||||
"lists",
|
||||
"list-methods",
|
||||
"numbers"
|
||||
],
|
||||
"difficulty": 2
|
||||
},
|
||||
{
|
||||
"slug": "twelve-days",
|
||||
"name": "Twelve Days",
|
||||
|
||||
54
exercises/practice/resistor-color-trio/.docs/instructions.md
Normal file
54
exercises/practice/resistor-color-trio/.docs/instructions.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# 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 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"` should return:
|
||||
|
||||
> "33 kiloohms"
|
||||
22
exercises/practice/resistor-color-trio/.meta/config.json
Normal file
22
exercises/practice/resistor-color-trio/.meta/config.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
|
||||
"authors": [
|
||||
"meabtall133",
|
||||
"bethanyg"
|
||||
],
|
||||
"contributors": [
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"resistor_color_trio.py"
|
||||
],
|
||||
"test": [
|
||||
"resistor_color_trio_test.py"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.py"
|
||||
]
|
||||
},
|
||||
"source": "Maud de Vries, Erik Schierboom",
|
||||
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
|
||||
}
|
||||
18
exercises/practice/resistor-color-trio/.meta/example.py
Normal file
18
exercises/practice/resistor-color-trio/.meta/example.py
Normal file
@@ -0,0 +1,18 @@
|
||||
COLORS = [
|
||||
'black',
|
||||
'brown',
|
||||
'red',
|
||||
'orange',
|
||||
'yellow',
|
||||
'green',
|
||||
'blue',
|
||||
'violet',
|
||||
'grey',
|
||||
'white'
|
||||
]
|
||||
|
||||
|
||||
def label(colors):
|
||||
value = 10 * COLORS.index(colors[0]) + COLORS.index(colors[1])
|
||||
value *= 10 ** COLORS.index(colors[2])
|
||||
return str(value) + ' ohms' if value < 1000 else str(value // 1000) + ' kiloohms'
|
||||
15
exercises/practice/resistor-color-trio/.meta/template.j2
Normal file
15
exercises/practice/resistor-color-trio/.meta/template.j2
Normal file
@@ -0,0 +1,15 @@
|
||||
{%- import "generator_macros.j2" as macros with context -%}
|
||||
{% macro test_case(case) -%}
|
||||
{%- set input = case["input"] -%}
|
||||
def test_{{ case["description"] | to_snake }}(self):
|
||||
self.assertEqual(
|
||||
{{ case["property"] | to_snake }}({{ case["input"]["colors"] }}),
|
||||
"{{ case['expected']['value']}} {{ case['expected']['unit']}}"
|
||||
)
|
||||
{%- endmacro %}
|
||||
{{ macros.header()}}
|
||||
|
||||
class {{ exercise | camel_case }}Test(unittest.TestCase):
|
||||
{% for case in cases -%}
|
||||
{{ test_case(case) }}
|
||||
{% endfor %}
|
||||
25
exercises/practice/resistor-color-trio/.meta/tests.toml
Normal file
25
exercises/practice/resistor-color-trio/.meta/tests.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
# 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"
|
||||
@@ -0,0 +1,2 @@
|
||||
def label(colors):
|
||||
pass
|
||||
@@ -0,0 +1,24 @@
|
||||
import unittest
|
||||
|
||||
from resistor_color_trio import (
|
||||
label,
|
||||
)
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json`
|
||||
|
||||
|
||||
class ResistorColorTrioTest(unittest.TestCase):
|
||||
def test_orange_and_orange_and_black(self):
|
||||
self.assertEqual(label(["orange", "orange", "black"]), "33 ohms")
|
||||
|
||||
def test_blue_and_grey_and_brown(self):
|
||||
self.assertEqual(label(["blue", "grey", "brown"]), "680 ohms")
|
||||
|
||||
def test_red_and_black_and_red(self):
|
||||
self.assertEqual(label(["red", "black", "red"]), "2 kiloohms")
|
||||
|
||||
def test_green_and_brown_and_orange(self):
|
||||
self.assertEqual(label(["green", "brown", "orange"]), "51 kiloohms")
|
||||
|
||||
def test_yellow_and_violet_and_yellow(self):
|
||||
self.assertEqual(label(["yellow", "violet", "yellow"]), "470 kiloohms")
|
||||
Reference in New Issue
Block a user