-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Resistor Color | ||
|
||
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. | ||
|
||
These colors are encoded as follows: | ||
|
||
- Black: 0 | ||
- Brown: 1 | ||
- Red: 2 | ||
- Orange: 3 | ||
- Yellow: 4 | ||
- Green: 5 | ||
- Blue: 6 | ||
- Violet: 7 | ||
- Grey: 8 | ||
- White: 9 | ||
|
||
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. | ||
|
||
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) | ||
|
||
## Exception messages | ||
|
||
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to | ||
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not | ||
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include | ||
a message. | ||
|
||
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of | ||
`raise Exception`, you should write: | ||
|
||
```python | ||
raise Exception("Meaningful message indicating the source of the error") | ||
``` | ||
|
||
## Running the tests | ||
|
||
To run the tests, run `pytest resistor_color_test.py` | ||
|
||
Alternatively, you can tell Python to run the pytest module: | ||
`python -m pytest resistor_color_test.py` | ||
|
||
### Common `pytest` options | ||
|
||
- `-v` : enable verbose output | ||
- `-x` : stop running tests on first failure | ||
- `--ff` : run failures from previous test before running other test cases | ||
|
||
For other options, see `python -m pytest -h` | ||
|
||
## Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/resistor-color` directory. | ||
|
||
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. | ||
|
||
For more detailed information about running tests, code style and linting, | ||
please see [Running the Tests](http://exercism.io/tracks/python/tests). | ||
|
||
## Source | ||
|
||
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458) | ||
|
||
## Submitting Incomplete Solutions | ||
|
||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
COLORS = [ | ||
"black", | ||
"brown", | ||
"red", | ||
"orange", | ||
"yellow", | ||
"green", | ||
"blue", | ||
"violet", | ||
"grey", | ||
"white", | ||
] | ||
COLORS_MAPPING = {name: idx for idx, name in enumerate(COLORS)} | ||
|
||
|
||
def color_code(color): | ||
return COLORS_MAPPING[color] | ||
|
||
|
||
def colors(): | ||
return COLORS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
|
||
from resistor_color import color_code, colors | ||
|
||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 | ||
|
||
|
||
class ResistorColorTest(unittest.TestCase): | ||
def test_black(self): | ||
self.assertEqual(color_code("black"), 0) | ||
|
||
def test_white(self): | ||
self.assertEqual(color_code("white"), 9) | ||
|
||
def test_orange(self): | ||
self.assertEqual(color_code("orange"), 3) | ||
|
||
def test_colors(self): | ||
expected = [ | ||
"black", | ||
"brown", | ||
"red", | ||
"orange", | ||
"yellow", | ||
"green", | ||
"blue", | ||
"violet", | ||
"grey", | ||
"white", | ||
] | ||
self.assertEqual(colors(), expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |