Skip to content

Commit

Permalink
Add resistor-color-duo exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 14, 2024
1 parent a03ce18 commit 4841fcf
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "a118a447-f41d-4904-8dfa-15f45ba3a990",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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.
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band 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

From the example above:
brown-green should return 15, and
brown-green-violet should return 15 too, ignoring the third color.
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"resistor-color-duo.R"
],
"test": [
"test_resistor-color-duo.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
}
5 changes: 5 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white") # nolint

value <- function(resistor_colors) {
sum((match(resistor_colors[1:2], colors) - 1) * c(10, 1))
}
31 changes: 31 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

[ce11995a-5b93-4950-a5e9-93423693b2fc]
description = "Brown and black"

[7bf82f7a-af23-48ba-a97d-38d59406a920]
description = "Blue and grey"

[f1886361-fdfd-4693-acf8-46726fe24e0c]
description = "Yellow and violet"

[b7a6cbd2-ae3c-470a-93eb-56670b305640]
description = "White and red"

[77a8293d-2a83-4016-b1af-991acc12b9fe]
description = "Orange and orange"

[0c4fb44f-db7c-4d03-afa8-054350f156a8]
description = "Ignore additional colors"

[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
description = "Black and brown, one-digit"
3 changes: 3 additions & 0 deletions exercises/practice/resistor-color-duo/resistor-color-duo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value <- function(resistor_colors) {

}
30 changes: 30 additions & 0 deletions exercises/practice/resistor-color-duo/test_resistor-color-duo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
source("./resistor-color-duo.R")
library(testthat)

test_that("Brown and black", {
expect_equal(value(c("brown", "black")), 10)
})

test_that("Blue and grey", {
expect_equal(value(c("blue", "grey")), 68)
})

test_that("Yellow and violet", {
expect_equal(value(c("yellow", "violet")), 47)
})

test_that("White and red", {
expect_equal(value(c("white", "red")), 92)
})

test_that("Orange and orange", {
expect_equal(value(c("orange", "orange")), 33)
})

test_that("Ignore additional colors", {
expect_equal(value(c("green", "brown", "orange")), 51)
})

test_that("Black and brown one digit", {
expect_equal(value(c("black", "brown")), 1)
})

0 comments on commit 4841fcf

Please sign in to comment.