Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "01cf8fd2-3e48-4b2a-b06f-7a1fdc42d561",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions exercises/practice/resistor-color-duo/.gitignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to ignore the JS files given they're just the transpiled ReScript code. It'd likely be confusing to have two copies of the test suite. This keeps it simple

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*/*.res.js
49 changes: 49 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/ResistorColorDuo.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
type band =
| Black
| Brown
| Red
| Orange
| Yellow
| Green
| Blue
| Violet
| Grey
| White

let bandToValue = (c: band) =>
switch c {
| Black => 0
| Brown => 1
| Red => 2
| Orange => 3
| Yellow => 4
| Green => 5
| Blue => 6
| Violet => 7
| Grey => 8
| White => 9
}

let stringToBand = (s: string) =>
switch s {
| "black" => Black
| "brown" => Brown
| "red" => Red
| "orange" => Orange
| "yellow" => Yellow
| "green" => Green
| "blue" => Blue
| "violet" => Violet
| "grey" => Grey
| "white" => White
| _ => Black
}

let transform = (opt: option<string>) =>
opt->Option.getOr("black")->stringToBand->bandToValue

let value = (colors: array<string>) => {
let tens = transform(colors[0])
let ones = transform(colors[1])
tens * 10 + ones
}
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/ResistorColorDuo.res"
],
"test": [
"tests/ResistorColorDuo_test.res"
],
"example": [
".meta/ResistorColorDuo.res"
],
"editor": [
"src/ResistorColorDuo.resi"
]
},
"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"
}
17 changes: 17 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/testTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { intEqual } from "../../../../test_generator/assertions.js";
import { generateTests } from '../../../../test_generator/testGenerator.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const slug = path.basename(path.resolve(__dirname, '..'))

// EDIT THIS WITH YOUR ASSERTIONS
export const assertionFunctions = [ intEqual ]

// EDIT THIS WITH YOUR TEST TEMPLATES
export const template = (c) => {
return `intEqual(~message="${c.description}", value(${JSON.stringify(c.input.colors)}), ${c.expected})`
}

generateTests(__dirname, slug, assertionFunctions, template)
31 changes: 31 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions exercises/practice/resistor-color-duo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading