Skip to content

Commit 29784b8

Browse files
committed
add: resistor-color
1 parent 6f0fabd commit 29784b8

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

python/resistor-color/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
## Exception messages
23+
24+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
25+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
26+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
27+
a message.
28+
29+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
30+
`raise Exception`, you should write:
31+
32+
```python
33+
raise Exception("Meaningful message indicating the source of the error")
34+
```
35+
36+
## Running the tests
37+
38+
To run the tests, run `pytest resistor_color_test.py`
39+
40+
Alternatively, you can tell Python to run the pytest module:
41+
`python -m pytest resistor_color_test.py`
42+
43+
### Common `pytest` options
44+
45+
- `-v` : enable verbose output
46+
- `-x` : stop running tests on first failure
47+
- `--ff` : run failures from previous test before running other test cases
48+
49+
For other options, see `python -m pytest -h`
50+
51+
## Submitting Exercises
52+
53+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/resistor-color` directory.
54+
55+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
56+
57+
For more detailed information about running tests, code style and linting,
58+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
59+
60+
## Source
61+
62+
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458)
63+
64+
## Submitting Incomplete Solutions
65+
66+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
COLORS = [
2+
"black",
3+
"brown",
4+
"red",
5+
"orange",
6+
"yellow",
7+
"green",
8+
"blue",
9+
"violet",
10+
"grey",
11+
"white",
12+
]
13+
COLORS_MAPPING = {name: idx for idx, name in enumerate(COLORS)}
14+
15+
16+
def color_code(color):
17+
return COLORS_MAPPING[color]
18+
19+
20+
def colors():
21+
return COLORS
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+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
6+
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)