From 05a3323c34d02e54ad98f768e92d3943dffaab15 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Tue, 15 Oct 2024 09:44:05 +0200 Subject: [PATCH] Add `resistor-color` exercise (#363) --- config.json | 8 ++++ .../resistor-color/.docs/instructions.md | 39 +++++++++++++++++++ .../practice/resistor-color/.meta/config.json | 19 +++++++++ .../practice/resistor-color/.meta/example.R | 5 +++ .../practice/resistor-color/.meta/tests.toml | 22 +++++++++++ .../practice/resistor-color/resistor-color.R | 5 +++ .../resistor-color/test_resistor-color.R | 19 +++++++++ 7 files changed, 117 insertions(+) create mode 100644 exercises/practice/resistor-color/.docs/instructions.md create mode 100644 exercises/practice/resistor-color/.meta/config.json create mode 100644 exercises/practice/resistor-color/.meta/example.R create mode 100644 exercises/practice/resistor-color/.meta/tests.toml create mode 100644 exercises/practice/resistor-color/resistor-color.R create mode 100644 exercises/practice/resistor-color/test_resistor-color.R diff --git a/config.json b/config.json index d746cc67..fb184240 100644 --- a/config.json +++ b/config.json @@ -737,6 +737,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "8af70f6c-85e2-41de-b5a3-20aaacb32a26", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "simple-cipher", "name": "Simple Cipher", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 00000000..0125e718 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.md @@ -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 diff --git a/exercises/practice/resistor-color/.meta/config.json b/exercises/practice/resistor-color/.meta/config.json new file mode 100644 index 00000000..dc014c86 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "resistor-color.R" + ], + "test": [ + "test_resistor-color.R" + ], + "example": [ + ".meta/example.R" + ] + }, + "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" +} diff --git a/exercises/practice/resistor-color/.meta/example.R b/exercises/practice/resistor-color/.meta/example.R new file mode 100644 index 00000000..469b584b --- /dev/null +++ b/exercises/practice/resistor-color/.meta/example.R @@ -0,0 +1,5 @@ +colors <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white") # nolint + +color_code <- function(color) { + match(color, colors) - 1 +} diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml new file mode 100644 index 00000000..9d4ee973 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/tests.toml @@ -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" diff --git a/exercises/practice/resistor-color/resistor-color.R b/exercises/practice/resistor-color/resistor-color.R new file mode 100644 index 00000000..41ad99cb --- /dev/null +++ b/exercises/practice/resistor-color/resistor-color.R @@ -0,0 +1,5 @@ +# TODO: define `colors` variable + +color_code <- function(color) { + +} diff --git a/exercises/practice/resistor-color/test_resistor-color.R b/exercises/practice/resistor-color/test_resistor-color.R new file mode 100644 index 00000000..15885a4e --- /dev/null +++ b/exercises/practice/resistor-color/test_resistor-color.R @@ -0,0 +1,19 @@ +source("./resistor-color.R") +library(testthat) + +test_that("Colors", { + expected <- c("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white") # nolint + expect_equal(colors, expected) +}) + +test_that("Black", { + expect_equal(color_code("black"), 0) +}) + +test_that("White", { + expect_equal(color_code("white"), 9) +}) + +test_that("Orange", { + expect_equal(color_code("orange"), 3) +})