From 84fba023cf5d862a1e3060ce88d73ff5d0eb967d Mon Sep 17 00:00:00 2001 From: Andras Nagy Date: Wed, 5 Jul 2023 08:19:46 -0700 Subject: [PATCH] Add resistor-color exercise --- config.json | 8 ++++ .../resistor-color/.docs/instructions.md | 39 +++++++++++++++++++ .../practice/resistor-color/.meta/config.json | 20 ++++++++++ .../practice/resistor-color/.meta/example.rkt | 12 ++++++ .../practice/resistor-color/.meta/tests.toml | 22 +++++++++++ .../resistor-color/resistor-color-test.rkt | 22 +++++++++++ .../resistor-color/resistor-color.rkt | 9 +++++ 7 files changed, 132 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.rkt create mode 100644 exercises/practice/resistor-color/.meta/tests.toml create mode 100644 exercises/practice/resistor-color/resistor-color-test.rkt create mode 100644 exercises/practice/resistor-color/resistor-color.rkt diff --git a/config.json b/config.json index 22926402..20249771 100644 --- a/config.json +++ b/config.json @@ -472,6 +472,14 @@ "strings" ] }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "278d9ae7-19da-449a-b2dd-56e3555d6d93", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "darts", "name": "Darts", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 00000000..6d47f819 --- /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 \ No newline at end of file diff --git a/exercises/practice/resistor-color/.meta/config.json b/exercises/practice/resistor-color/.meta/config.json new file mode 100644 index 00000000..b84fc04e --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,20 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "resistor-color.rkt" + ], + "test": [ + "resistor-color-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "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" + } + \ No newline at end of file diff --git a/exercises/practice/resistor-color/.meta/example.rkt b/exercises/practice/resistor-color/.meta/example.rkt new file mode 100644 index 00000000..8983dc7c --- /dev/null +++ b/exercises/practice/resistor-color/.meta/example.rkt @@ -0,0 +1,12 @@ +#lang racket + +(provide color-code colors) + +(define resistor-colors + (vector "black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white")) + +(define (color-code color) + (vector-member color resistor-colors)) + +(define (colors) + (vector->list resistor-colors)) 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-test.rkt b/exercises/practice/resistor-color/resistor-color-test.rkt new file mode 100644 index 00000000..afa789a8 --- /dev/null +++ b/exercises/practice/resistor-color/resistor-color-test.rkt @@ -0,0 +1,22 @@ +#lang racket/base + +; Tests adapted from `problem-specifications/canonical-data.json v1.1.0 +(require "resistor-color.rkt") + +(module+ test + (require rackunit rackunit/text-ui) + + (run-tests + (test-suite "resistor-color tests" + (test-equal? "Color codes -> Black" + (color-code "black") + 0) + (test-equal? "Color codes -> White" + (color-code "white") + 9) + (test-equal? "Color codes -> Orange" + (color-code "orange") + 3) + (test-equal? "Colors" + (colors) + '("black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white"))))) diff --git a/exercises/practice/resistor-color/resistor-color.rkt b/exercises/practice/resistor-color/resistor-color.rkt new file mode 100644 index 00000000..e9aa1860 --- /dev/null +++ b/exercises/practice/resistor-color/resistor-color.rkt @@ -0,0 +1,9 @@ +#lang racket + +(provide color-code colors) + +(define (color-code color) + (error "Not implemented yet")) + +(define (colors) + (error "Not implemented yet")) \ No newline at end of file