Skip to content

Commit

Permalink
add: resistor-color
Browse files Browse the repository at this point in the history
  • Loading branch information
ibLeDy committed Aug 19, 2021
1 parent 6f0fabd commit 29784b8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
66 changes: 66 additions & 0 deletions python/resistor-color/README.md
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.
21 changes: 21 additions & 0 deletions python/resistor-color/resistor_color.py
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
35 changes: 35 additions & 0 deletions python/resistor-color/resistor_color_test.py
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()

0 comments on commit 29784b8

Please sign in to comment.