Skip to content

Commit

Permalink
Add resistor-color exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jan 6, 2024
1 parent 91ea7c8 commit a9d08f8
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@
"math"
]
},
{
"slug": "resistor-color",
"name": "Resistor Color",
"uuid": "6f128832-f1b8-4a35-85b1-61c4d487afde",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rna-transcription",
"name": "Rna Transcription",
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/resistor-color/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Instructions

If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
For this exercise, you need to know two things about them:

- Each resistor has a resistance value.
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.

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

The goal of this exercise is to create a way:

- to look up the numerical value associated with a particular color band
- to list the different band colors

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][e-color-code].

[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"resistor-color.coffee"
],
"test": [
"resistor-color.spec.coffee"
],
"example": [
".meta/example.coffee"
]
},
"blurb": "Convert a resistor band's color to its numeric representation.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
}
21 changes: 21 additions & 0 deletions exercises/practice/resistor-color/.meta/example.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ResistorColor
@colorBands: [
"black"
"brown"
"red"
"orange"
"yellow"
"green"
"blue"
"violet"
"grey"
"white"
]

@colorCode: (color) ->
@colorBands.indexOf(color)

@colors: () ->
@colorBands

module.exports = ResistorColor
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[49eb31c5-10a8-4180-9f7f-fea632ab87ef]
description = "Color codes -> Black"

[0a4df94b-92da-4579-a907-65040ce0b3fc]
description = "Color codes -> White"

[5f81608d-f36f-4190-8084-f45116b6f380]
description = "Color codes -> Orange"

[581d68fa-f968-4be2-9f9d-880f2fb73cf7]
description = "Colors"
6 changes: 6 additions & 0 deletions exercises/practice/resistor-color/resistor-color.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ResistorColor
@colorCode: (color) ->

@colors: () ->

module.exports = ResistorColor
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color/resistor-color.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ResistorColor = require './resistor-color'

describe 'ResistorColor', ->
it 'Black', ->
results = ResistorColor.colorCode('black')
expected = 0
expect(results).toEqual expected

xit 'White', ->
results = ResistorColor.colorCode('white')
expected = 9
expect(results).toEqual expected

xit 'Orange', ->
results = ResistorColor.colorCode('orange')
expected = 3
expect(results).toEqual expected

xit 'Colors', ->
results = ResistorColor.colors()
expected = [
'black'
'brown'
'red'
'orange'
'yellow'
'green'
'blue'
'violet'
'grey'
'white'
]
expect(results).toEqual expected

0 comments on commit a9d08f8

Please sign in to comment.