From ca99f6eba674db9bea4c7e4c76f6a45a33871859 Mon Sep 17 00:00:00 2001 From: Andras Nagy Date: Wed, 14 Jun 2023 11:32:13 -0700 Subject: [PATCH 1/2] Initial implementation --- config.json | 8 +++++ .../resistor-color-duo/.docs/instructions.md | 33 +++++++++++++++++++ .../resistor-color-duo/.meta/config.json | 19 +++++++++++ .../resistor-color-duo/.meta/example.rkt | 13 ++++++++ .../resistor-color-duo/.meta/tests.toml | 31 +++++++++++++++++ .../resistor-color-duo-test.rkt | 31 +++++++++++++++++ .../resistor-color-duo/resistor-color-duo.rkt | 6 ++++ 7 files changed, 141 insertions(+) create mode 100644 exercises/practice/resistor-color-duo/.docs/instructions.md create mode 100644 exercises/practice/resistor-color-duo/.meta/config.json create mode 100644 exercises/practice/resistor-color-duo/.meta/example.rkt create mode 100644 exercises/practice/resistor-color-duo/.meta/tests.toml create mode 100644 exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt create mode 100644 exercises/practice/resistor-color-duo/resistor-color-duo.rkt diff --git a/config.json b/config.json index 22926402..e66f1df6 100644 --- a/config.json +++ b/config.json @@ -479,6 +479,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "resistor-color-duo", + "name": "Resistor Color Duo", + "uuid": "c6eff45a-b188-4689-a2fa-96b036ec04ac", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, diff --git a/exercises/practice/resistor-color-duo/.docs/instructions.md b/exercises/practice/resistor-color-duo/.docs/instructions.md new file mode 100644 index 00000000..bdcd549b --- /dev/null +++ b/exercises/practice/resistor-color-duo/.docs/instructions.md @@ -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 +brown-green-violet should return 15 too, ignoring the third color. diff --git a/exercises/practice/resistor-color-duo/.meta/config.json b/exercises/practice/resistor-color-duo/.meta/config.json new file mode 100644 index 00000000..093615b4 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "resistor-color-duo.rkt" + ], + "test": [ + "resistor-color-duo-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "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" +} diff --git a/exercises/practice/resistor-color-duo/.meta/example.rkt b/exercises/practice/resistor-color-duo/.meta/example.rkt new file mode 100644 index 00000000..4e3a15ea --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/example.rkt @@ -0,0 +1,13 @@ +#lang racket + +(define resistor-colors + '#(black brown red orange yellow green blue violet grey white)) + +(define (color-code colors) + (match colors + [(list first second _ ...) + (let* ([tens (vector-member first resistor-colors)] + [ones (vector-member second resistor-colors)] + [value (+ (* tens 10) ones)]) + value)] + [ _ (error "Invalid colors argument")])) diff --git a/exercises/practice/resistor-color-duo/.meta/tests.toml b/exercises/practice/resistor-color-duo/.meta/tests.toml new file mode 100644 index 00000000..9036fc78 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/tests.toml @@ -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" diff --git a/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt b/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt new file mode 100644 index 00000000..84bbd00f --- /dev/null +++ b/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt @@ -0,0 +1,31 @@ +#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? "Brown and black" + (color-code '(brown black)) + 10) + (test-equal? "Blue and grey" + (color-code '(blue grey)) + 68) + (test-equal? "Yellow and violet" + (color-code '(yellow violet)) + 47) + (test-equal? "White and red" + (color-code '(white red)) + 92) + (test-equal? "Orange and orange" + (color-code '(orange orange)) + 33) + (test-equal? "Ignore additional colors" + (color-code '(green brown orange)) + 51) + (test-equal? "Black and brown, one-digit" + (color-code '(brown brown)) + 1)))) diff --git a/exercises/practice/resistor-color-duo/resistor-color-duo.rkt b/exercises/practice/resistor-color-duo/resistor-color-duo.rkt new file mode 100644 index 00000000..cfbe590b --- /dev/null +++ b/exercises/practice/resistor-color-duo/resistor-color-duo.rkt @@ -0,0 +1,6 @@ +#lang racket + +(provide color-code) + +(define (color-code color) + (error "Not implemented yet")) From 1ba70bc0884579e33d31e3771dec525e1b014ed1 Mon Sep 17 00:00:00 2001 From: Andras Nagy Date: Thu, 15 Jun 2023 22:15:57 -0700 Subject: [PATCH 2/2] Fixed errors --- .../resistor-color-duo/.meta/example.rkt | 4 +++- .../resistor-color-duo-test.rkt | 17 ++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/exercises/practice/resistor-color-duo/.meta/example.rkt b/exercises/practice/resistor-color-duo/.meta/example.rkt index 4e3a15ea..9ea0d84b 100644 --- a/exercises/practice/resistor-color-duo/.meta/example.rkt +++ b/exercises/practice/resistor-color-duo/.meta/example.rkt @@ -1,7 +1,9 @@ #lang racket +(provide color-code) + (define resistor-colors - '#(black brown red orange yellow green blue violet grey white)) + '#("black" "brown" "red" "orange" "yellow" "green" "blue" "violet" "grey" "white")) (define (color-code colors) (match colors diff --git a/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt b/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt index 84bbd00f..e94fb2a2 100644 --- a/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt +++ b/exercises/practice/resistor-color-duo/resistor-color-duo-test.rkt @@ -1,7 +1,6 @@ #lang racket/base -; Tests adapted from `problem-specifications/canonical-data.json v1.1.0 -(require "resistor-color.rkt") +(require "resistor-color-duo.rkt") (module+ test (require rackunit rackunit/text-ui) @@ -9,23 +8,23 @@ (run-tests (test-suite "resistor-color tests" (test-equal? "Brown and black" - (color-code '(brown black)) + (color-code '("brown" "black")) 10) (test-equal? "Blue and grey" - (color-code '(blue grey)) + (color-code '("blue" "grey")) 68) (test-equal? "Yellow and violet" - (color-code '(yellow violet)) + (color-code '("yellow" "violet")) 47) (test-equal? "White and red" - (color-code '(white red)) + (color-code '("white" "red")) 92) (test-equal? "Orange and orange" - (color-code '(orange orange)) + (color-code '("orange" "orange")) 33) (test-equal? "Ignore additional colors" - (color-code '(green brown orange)) + (color-code '("green" "brown" "orange")) 51) (test-equal? "Black and brown, one-digit" - (color-code '(brown brown)) + (color-code '("black" "brown")) 1))))