From edb64e458e85c313799dd066cffbe212c002a2e5 Mon Sep 17 00:00:00 2001 From: Andras Nagy Date: Thu, 15 Jun 2023 15:41:04 -0700 Subject: [PATCH 1/4] Initial implementation --- config.json | 8 +++ .../resistor-color-trio/.docs/instructions.md | 56 +++++++++++++++++++ .../resistor-color-trio/.meta/config.json | 19 +++++++ .../resistor-color-trio/.meta/example.rkt | 24 ++++++++ .../resistor-color-trio/.meta/tests.toml | 40 +++++++++++++ .../resistor-color-trio-test.rkt | 39 +++++++++++++ .../resistor-color-trio.rkt | 6 ++ 7 files changed, 192 insertions(+) create mode 100644 exercises/practice/resistor-color-trio/.docs/instructions.md create mode 100644 exercises/practice/resistor-color-trio/.meta/config.json create mode 100644 exercises/practice/resistor-color-trio/.meta/example.rkt create mode 100644 exercises/practice/resistor-color-trio/.meta/tests.toml create mode 100644 exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt create mode 100644 exercises/practice/resistor-color-trio/resistor-color-trio.rkt diff --git a/config.json b/config.json index 22926402..d633bfe2 100644 --- a/config.json +++ b/config.json @@ -471,6 +471,14 @@ "recursion", "strings" ] + }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "bffafc9e-379f-44f3-81b4-1bbb9787c1fe", + "practices": [], + "prerequisites": [], + "difficulty": 1, }, { "slug": "darts", diff --git a/exercises/practice/resistor-color-trio/.docs/instructions.md b/exercises/practice/resistor-color-trio/.docs/instructions.md new file mode 100644 index 00000000..4ad2aede --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.md @@ -0,0 +1,56 @@ +# Instructions + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. +For this exercise, you need to know only three 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 acts as a digit of a 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 3 colors as input, and outputs the correct value, in ohms. + The color bands are encoded as follows: + +- Black: 0 +- Brown: 1 +- Red: 2 +- Orange: 3 +- Yellow: 4 +- Green: 5 +- Blue: 6 +- Violet: 7 +- Grey: 8 +- White: 9 + +In `resistor-color duo` you decoded the first two colors. +For instance: orange-orange got the main value `33`. +The third color stands for how many zeros need to be added to the main value. +The main value plus the zeros gives us a value in ohms. +For the exercise it doesn't matter what ohms really are. +For example: + +- orange-orange-black would be 33 and no zeros, which becomes 33 ohms. +- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms. +- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms. + +(If Math is your thing, you may want to think of the zeros as exponents of 10. +If Math is not your thing, go with the zeros. +It really is the same thing, just in plain English instead of Math lingo.) + +This exercise is about translating the colors into a label: + +> "... ohms" + +So an input of `"orange", "orange", "black"` should return: + +> "33 ohms" + +When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms". +That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams". + +For example, an input of `"orange", "orange", "orange"` should return: + +> "33 kiloohms" + +[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix diff --git a/exercises/practice/resistor-color-trio/.meta/config.json b/exercises/practice/resistor-color-trio/.meta/config.json new file mode 100644 index 00000000..f882c58c --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "resistor-color-trio.rkt" + ], + "test": [ + "resistor-color-trio-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "blurb": "Convert color codes, as used on resistors, to a human-readable label.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1549" +} diff --git a/exercises/practice/resistor-color-trio/.meta/example.rkt b/exercises/practice/resistor-color-trio/.meta/example.rkt new file mode 100644 index 00000000..fe174bc1 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/example.rkt @@ -0,0 +1,24 @@ +#lang racket + +(provide color-code) + +(define resistor-colors + '#(black brown red orange yellow green blue violet grey white)) + +(define (color-code colors) + (match colors + [(list first second third rest ...) + (let* ([index-of (lambda (elem) (vector-member elem resistor-colors))] + [tens (index-of first)] + [ones (index-of second)] + [pow (index-of third)] + [initial (+ (* tens 10) ones)] + [value (* initial (expt 10 pow))] + [magnitude (cond [(< value 1000) 0] + [(< value 1000000) 1] + [(< value 1000000000) 2] + [else 3])] + [prefix (vector-ref '#("" "kilo" "mega" "giga") magnitude)] + [scaled-value (/ value (expt 10 (* magnitude 3)))]) + (format "~a ~aohms" scaled-value prefix))] + [_ (error "Invalid colors argument")])) diff --git a/exercises/practice/resistor-color-trio/.meta/tests.toml b/exercises/practice/resistor-color-trio/.meta/tests.toml new file mode 100644 index 00000000..b7d45fa5 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/tests.toml @@ -0,0 +1,40 @@ +# 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. + +[d6863355-15b7-40bb-abe0-bfb1a25512ed] +description = "Orange and orange and black" + +[1224a3a9-8c8e-4032-843a-5224e04647d6] +description = "Blue and grey and brown" + +[b8bda7dc-6b95-4539-abb2-2ad51d66a207] +description = "Red and black and red" + +[5b1e74bc-d838-4eda-bbb3-eaba988e733b] +description = "Green and brown and orange" + +[f5d37ef9-1919-4719-a90d-a33c5a6934c9] +description = "Yellow and violet and yellow" + +[5f6404a7-5bb3-4283-877d-3d39bcc33854] +description = "Blue and violet and blue" + +[7d3a6ab8-e40e-46c3-98b1-91639fff2344] +description = "Minimum possible value" + +[ca0aa0ac-3825-42de-9f07-dac68cc580fd] +description = "Maximum possible value" + +[0061a76c-903a-4714-8ce2-f26ce23b0e09] +description = "First two colors make an invalid octal number" + +[30872c92-f567-4b69-a105-8455611c10c4] +description = "Ignore extra colors" diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt b/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt new file mode 100644 index 00000000..4cb35e27 --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt @@ -0,0 +1,39 @@ +#lang racket/base + +(require "resistor-color-trio.rkt") + +(module+ test + (require rackunit rackunit/text-ui) + + (run-tests + (test-suite "resistor-color tests" + (test-equal? "Orange and orange and black" + (color-code '(orange orange black)) + "33 ohms") + (test-equal? "Blue and grey and brown" + (color-code '(blue grey brown)) + "680 ohms") + (test-equal? "Red and black and red" + (color-code '(red black red)) + "2 kiloohms") + (test-equal? "Green and brown and orange" + (color-code '(green brown orange)) + "51 kiloohms") + (test-equal? "Yellow and violet and yellow" + (color-code '(yellow violet yellow)) + "470 kiloohms") + (test-equal? "Blue and violet and blue" + (color-code '(blue violet blue)) + "67 megaohms") + (test-equal? "Minimum possible value" + (color-code '(black black black)) + "0 ohms") + (test-equal? "Maximum possible value" + (color-code '(white white white)) + "99 gigaohms") + (test-equal? "First two colors make an invalid octal number" + (color-code '(black grey black)) + "8 ohms") + (test-equal? "Ignore extra colors" + (color-code '(blue green yellow orange)) + "650 kiloohms")))) diff --git a/exercises/practice/resistor-color-trio/resistor-color-trio.rkt b/exercises/practice/resistor-color-trio/resistor-color-trio.rkt new file mode 100644 index 00000000..2b4c44e5 --- /dev/null +++ b/exercises/practice/resistor-color-trio/resistor-color-trio.rkt @@ -0,0 +1,6 @@ +#lang racket + +(provide color-code) + +(define (color-code colors) + (error "Not implemented yet")) From 9966417a51a5dee1711933dbadc36e95a4e67c07 Mon Sep 17 00:00:00 2001 From: Andras Nagy Date: Wed, 5 Jul 2023 08:38:11 -0700 Subject: [PATCH 2/4] Use string arguments --- .../resistor-color-trio/.meta/example.rkt | 2 +- .../resistor-color-trio-test.rkt | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/exercises/practice/resistor-color-trio/.meta/example.rkt b/exercises/practice/resistor-color-trio/.meta/example.rkt index fe174bc1..01634090 100644 --- a/exercises/practice/resistor-color-trio/.meta/example.rkt +++ b/exercises/practice/resistor-color-trio/.meta/example.rkt @@ -3,7 +3,7 @@ (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-trio/resistor-color-trio-test.rkt b/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt index 4cb35e27..e261a67a 100644 --- a/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt +++ b/exercises/practice/resistor-color-trio/resistor-color-trio-test.rkt @@ -8,32 +8,32 @@ (run-tests (test-suite "resistor-color tests" (test-equal? "Orange and orange and black" - (color-code '(orange orange black)) + (color-code '("orange" "orange" "black")) "33 ohms") (test-equal? "Blue and grey and brown" - (color-code '(blue grey brown)) + (color-code '("blue" "grey" "brown")) "680 ohms") (test-equal? "Red and black and red" - (color-code '(red black red)) + (color-code '("red" "black" "red")) "2 kiloohms") (test-equal? "Green and brown and orange" - (color-code '(green brown orange)) + (color-code '("green" "brown" "orange")) "51 kiloohms") (test-equal? "Yellow and violet and yellow" - (color-code '(yellow violet yellow)) + (color-code '("yellow" "violet" "yellow")) "470 kiloohms") (test-equal? "Blue and violet and blue" - (color-code '(blue violet blue)) + (color-code '("blue" "violet" "blue")) "67 megaohms") (test-equal? "Minimum possible value" - (color-code '(black black black)) + (color-code '("black" "black" "black")) "0 ohms") (test-equal? "Maximum possible value" - (color-code '(white white white)) + (color-code '("white" "white" "white")) "99 gigaohms") (test-equal? "First two colors make an invalid octal number" - (color-code '(black grey black)) + (color-code '("black" "grey" "black")) "8 ohms") (test-equal? "Ignore extra colors" - (color-code '(blue green yellow orange)) + (color-code '("blue" "green" "yellow" "orange")) "650 kiloohms")))) From e5e5e8d0c2470e697d35ff5733e7f83567d965a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 29 Aug 2023 07:14:36 -0700 Subject: [PATCH 3/4] Update config.json Co-authored-by: Erik Schierboom --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index d633bfe2..0099159f 100644 --- a/config.json +++ b/config.json @@ -472,7 +472,7 @@ "strings" ] }, - { + { "slug": "resistor-color-trio", "name": "Resistor Color Trio", "uuid": "bffafc9e-379f-44f3-81b4-1bbb9787c1fe", From 6a5b07b5cb3056b8b73d7808be907f856bbcf350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:08:41 -0700 Subject: [PATCH 4/4] Fix trailing comma --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 0e3d438b..05f16c73 100644 --- a/config.json +++ b/config.json @@ -478,7 +478,7 @@ "uuid": "bffafc9e-379f-44f3-81b4-1bbb9787c1fe", "practices": [], "prerequisites": [], - "difficulty": 1, + "difficulty": 1 }, { "slug": "darts",