Skip to content

Commit db28e58

Browse files
meatball133BethanyG
authored andcommitted
Added resistor_color_trio_test
1 parent 23ab8d8 commit db28e58

8 files changed

Lines changed: 174 additions & 0 deletions

File tree

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,20 @@
682682
],
683683
"difficulty": 2
684684
},
685+
{
686+
"slug": "resistor-color-trio",
687+
"name": "Resistor Color Trio",
688+
"uuid": "089f06a6-0759-479c-8c00-d699525a1e22",
689+
"practices": ["list-methods"],
690+
"prerequisites": [
691+
"basics",
692+
"bools",
693+
"lists",
694+
"list-methods",
695+
"numbers"
696+
],
697+
"difficulty": 2
698+
},
685699
{
686700
"slug": "twelve-days",
687701
"name": "Twelve Days",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know only three things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
9+
- Each band acts as a digit of a number.
10+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
11+
In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands.
12+
The program will take 3 colors as input, and outputs the correct value, in ohms.
13+
The color bands are encoded as follows:
14+
15+
- Black: 0
16+
- Brown: 1
17+
- Red: 2
18+
- Orange: 3
19+
- Yellow: 4
20+
- Green: 5
21+
- Blue: 6
22+
- Violet: 7
23+
- Grey: 8
24+
- White: 9
25+
26+
In `resistor-color duo` you decoded the first two colors.
27+
For instance: orange-orange got the main value `33`.
28+
The third color stands for how many zeros need to be added to the main value.
29+
The main value plus the zeros gives us a value in ohms.
30+
For the exercise it doesn't matter what ohms really are.
31+
For example:
32+
33+
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
34+
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
35+
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
36+
37+
(If Math is your thing, you may want to think of the zeros as exponents of 10.
38+
If Math is not your thing, go with the zeros.
39+
It really is the same thing, just in plain English instead of Math lingo.)
40+
41+
This exercise is about translating the colors into a label:
42+
43+
> "... ohms"
44+
45+
So an input of `"orange", "orange", "black"` should return:
46+
47+
> "33 ohms"
48+
49+
When we get more than a thousand ohms, we say "kiloohms".
50+
That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.
51+
52+
So an input of `"orange", "orange", "orange"` should return:
53+
54+
> "33 kiloohms"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
3+
"authors": [
4+
"meabtall133",
5+
"bethanyg"
6+
],
7+
"contributors": [
8+
],
9+
"files": {
10+
"solution": [
11+
"resistor_color_trio.py"
12+
],
13+
"test": [
14+
"resistor_color_trio_test.py"
15+
],
16+
"example": [
17+
".meta/example.py"
18+
]
19+
},
20+
"source": "Maud de Vries, Erik Schierboom",
21+
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
COLORS = [
2+
'black',
3+
'brown',
4+
'red',
5+
'orange',
6+
'yellow',
7+
'green',
8+
'blue',
9+
'violet',
10+
'grey',
11+
'white'
12+
]
13+
14+
15+
def label(colors):
16+
value = 10 * COLORS.index(colors[0]) + COLORS.index(colors[1])
17+
value *= 10 ** COLORS.index(colors[2])
18+
return str(value) + ' ohms' if value < 1000 else str(value // 1000) + ' kiloohms'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{% macro test_case(case) -%}
3+
{%- set input = case["input"] -%}
4+
def test_{{ case["description"] | to_snake }}(self):
5+
self.assertEqual(
6+
{{ case["property"] | to_snake }}({{ case["input"]["colors"] }}),
7+
"{{ case['expected']['value']}} {{ case['expected']['unit']}}"
8+
)
9+
{%- endmacro %}
10+
{{ macros.header()}}
11+
12+
class {{ exercise | camel_case }}Test(unittest.TestCase):
13+
{% for case in cases -%}
14+
{{ test_case(case) }}
15+
{% endfor %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[d6863355-15b7-40bb-abe0-bfb1a25512ed]
13+
description = "Orange and orange and black"
14+
15+
[1224a3a9-8c8e-4032-843a-5224e04647d6]
16+
description = "Blue and grey and brown"
17+
18+
[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
19+
description = "Red and black and red"
20+
21+
[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
22+
description = "Green and brown and orange"
23+
24+
[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
25+
description = "Yellow and violet and yellow"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def label(colors):
2+
pass
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from resistor_color_trio import (
4+
label,
5+
)
6+
7+
# Tests adapted from `problem-specifications//canonical-data.json`
8+
9+
10+
class ResistorColorTrioTest(unittest.TestCase):
11+
def test_orange_and_orange_and_black(self):
12+
self.assertEqual(label(["orange", "orange", "black"]), "33 ohms")
13+
14+
def test_blue_and_grey_and_brown(self):
15+
self.assertEqual(label(["blue", "grey", "brown"]), "680 ohms")
16+
17+
def test_red_and_black_and_red(self):
18+
self.assertEqual(label(["red", "black", "red"]), "2 kiloohms")
19+
20+
def test_green_and_brown_and_orange(self):
21+
self.assertEqual(label(["green", "brown", "orange"]), "51 kiloohms")
22+
23+
def test_yellow_and_violet_and_yellow(self):
24+
self.assertEqual(label(["yellow", "violet", "yellow"]), "470 kiloohms")

0 commit comments

Comments
 (0)