Skip to content

Commit 7dec1a1

Browse files
gabriel376cmccandless
authored andcommitted
implement exercise resistor-color (exercism#1680) (exercism#1686)
closes exercism#1680
1 parent 817a864 commit 7dec1a1

5 files changed

Lines changed: 141 additions & 0 deletions

File tree

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,16 @@
15341534
"difficulty": 0,
15351535
"topics": null,
15361536
"deprecated": true
1537+
},
1538+
{
1539+
"slug": "resistor-color",
1540+
"uuid": "d17bee9c-e803-4745-85ea-864f255fb04e",
1541+
"core": false,
1542+
"unlocked_by": null,
1543+
"difficulty": 1,
1544+
"topics": [
1545+
"arrays"
1546+
]
15371547
}
15381548
]
15391549
}

exercises/resistor-color/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Resistor Color
2+
3+
Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
4+
5+
These colors are encoded as follows:
6+
7+
- Black: 0
8+
- Brown: 1
9+
- Red: 2
10+
- Orange: 3
11+
- Yellow: 4
12+
- Green: 5
13+
- Blue: 6
14+
- Violet: 7
15+
- Grey: 8
16+
- White: 9
17+
18+
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.
19+
20+
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code)
21+
22+
23+
## Exception messages
24+
25+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
26+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
27+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
28+
a message.
29+
30+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
31+
`raise Exception`, you should write:
32+
33+
```python
34+
raise Exception("Meaningful message indicating the source of the error")
35+
```
36+
37+
## Running the tests
38+
39+
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
40+
41+
- Python 2.7: `py.test resistor_color_test.py`
42+
- Python 3.4+: `pytest resistor_color_test.py`
43+
44+
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
45+
`python -m pytest resistor_color_test.py`
46+
47+
### Common `pytest` options
48+
49+
- `-v` : enable verbose output
50+
- `-x` : stop running tests on first failure
51+
- `--ff` : run failures from previous test before running other test cases
52+
53+
For other options, see `python -m pytest -h`
54+
55+
## Submitting Exercises
56+
57+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/resistor-color` directory.
58+
59+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
60+
61+
For more detailed information about running tests, code style and linting,
62+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
63+
64+
## Source
65+
66+
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458)
67+
68+
## Submitting Incomplete Solutions
69+
70+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 color_code(color):
16+
return COLORS.index(color)
17+
18+
19+
def colors():
20+
return COLORS
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def color_code(color):
2+
pass
3+
4+
5+
def colors():
6+
pass
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
from resistor_color import color_code, colors
4+
5+
6+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
7+
8+
class ResistorColorTest(unittest.TestCase):
9+
def test_black(self):
10+
self.assertEqual(color_code('black'), 0)
11+
12+
def test_white(self):
13+
self.assertEqual(color_code('white'), 9)
14+
15+
def test_orange(self):
16+
self.assertEqual(color_code('orange'), 3)
17+
18+
def test_colors(self):
19+
expected = [
20+
'black',
21+
'brown',
22+
'red',
23+
'orange',
24+
'yellow',
25+
'green',
26+
'blue',
27+
'violet',
28+
'grey',
29+
'white'
30+
]
31+
self.assertEqual(colors(), expected)
32+
33+
34+
if __name__ == '__main__':
35+
unittest.main()

0 commit comments

Comments
 (0)