Add resistor-color-duo exercise (#217)

This commit is contained in:
Erik Schierboom
2025-01-30 19:49:02 +01:00
committed by GitHub
parent 97e875b015
commit ab9fd978fc
8 changed files with 323 additions and 0 deletions

View File

@@ -157,6 +157,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "5e8ed224-3711-4444-98e7-03880d95021a",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "luhn",
"name": "Luhn Credit Card Number Checker",

View File

@@ -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.

View File

@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"resistor-color-duo.8th"
],
"test": [
"test.8th"
],
"example": [
".meta/example.8th"
]
},
"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"
}

View File

@@ -0,0 +1,11 @@
: colors \ -- a
["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]
;
: color-code \ s -- n
colors swap ' s:= a:indexof nip
;
: value \ a -- n
[10, 1] swap ( color-code n:* ) a:2map ' n:+ 0 a:reduce
;

View File

@@ -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"

View File

@@ -0,0 +1,174 @@
needs console/loaded
needs utils/latebind
\ -----------------------------------------------------------------
ns: test
-1 var, test-count
var tests-passed
var tests-failed
var tests-skipped
true var, run-test
\ Some utility words
: test-passed \ s x x -- \\ test name, expected value, actual value
2drop
1 tests-passed n:+!
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
;
: test-skipped \ s --
1 tests-skipped n:+!
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
;
: test-failed \ s x x -- \\ test name, expected value, actual value
1 tests-failed n:+!
rot
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
" Actual: «" . . "»" . cr
" Expected: «" . . "»" . cr cr
;
: isword? \ x -- x f
dup >kind ns:w n:=
;
: run-test? \ -- T
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
;
\ Num passed + num skipped + num failed should == num tests
: all-tests-run? \ -- T
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
test-count @ n:=
;
\ returns true if x is a date, false otherwise
: date? \ x -- x T
dup >kind ns:d n:=
;
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
: eq? \ x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
\ same kind: try different comparators
number? if n:= ;then
string? if s:= ;then
array? if ' eq? a:= 2nip ;then
map? if ' eq? m:= 2nip ;then
date? if d:= ;then
\ otherwise fall back to 'lazy evaluation'
G:l: =
;
: eps_eq? \ n x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
number? !if 2drop false ;then
rot n:~=
;
: check-depth \ ... n -- ...
dup>r
n:1+ depth n:=
!if
con:red con:onBlack
"PANIC: expected stack depth to be " . r> . cr
"Stack is:" . cr
.s cr
255 die
then
rdrop
;
\ -----------------------------------------------------------------
\ status report at end of run
( all-tests-run?
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
) onexit
\ Print a summary of the tests run
( con:white con:onBlack
test-count @ . space "tests planned - " .
tests-passed @ . space "passed - " .
tests-skipped @ . space "skipped - " .
tests-failed @ . space "failed" . cr
) onexit
\ -----------------------------------------------------------------
\ The public-facing words
\ -----------------------------------------------------------------
: equal? \ s x w -- | s w x --
run-test? !if 2drop test-skipped ;; then
isword? !if swap then
w:exec
3 check-depth
2dup \ so test-failed can show actual and expected
eq? if test-passed else test-failed then
;
: approx_equal? \ s x w n -- | s w x n --
run-test? !if 3drop test-skipped ;; then
-rot isword? !if swap then
w:exec
4 check-depth
3dup \ so test-failed can show actual and expected
eps_eq?
if rot drop test-passed else rot drop test-failed then
;
: true? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
true swap dup \ so test-failed can show actual and expected
if test-passed else test-failed then
;
: false? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
false swap dup \ so test-failed can show actual and expected
!if test-passed else test-failed then
;
: null? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
null swap dup \ so test-failed can show actual and expected
G:null? nip if test-passed else test-failed then
;
: SKIP-REST-OF-TESTS false run-test ! ;
: tests \ n --
test-count !
;
\ Set the exit status:
\ 0 = all OK
\ 1 = not all tests were run (some error occurred)
\ 2 = some tests failed
: end-of-tests \ --
all-tests-run?
if
tests-failed @ 0 n:= if 0 else 2 then
else
1
then
die
;

View File

@@ -0,0 +1,3 @@
: value \ a -- n
;

View File

@@ -0,0 +1,44 @@
"resistor-color-duo.8th" f:include
needs exercism/test
with: test
7 tests
"Brown and black"
( ["brown", "black"] value )
10
equal?
SKIP-REST-OF-TESTS
"Blue and grey"
( ["blue", "grey"] value )
68
equal?
"Yellow and violet"
( ["yellow", "violet"] value )
47
equal?
"White and red"
( ["white", "red"] value )
92
equal?
"Orange and orange"
( ["orange", "orange"] value )
33
equal?
"Ignore additional colors"
( ["green", "brown", "orange"] value )
51
equal?
"Black and brown, one-digit"
( ["black", "brown"] value )
1
equal?
end-of-tests
;with