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 @@ -217,6 +217,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "e4b8a28c-56e7-4fec-9fe9-d52902743153",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
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.
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"resistor_color_duo.fut"
],
"test": [
"test.fut"
],
"example": [
".meta/example.fut"
]
},
"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"
}
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def equal [m] [n] (first: [m]u8) (second: [n]u8): bool =
if m != n then false else
let same = loop same = true for i < n do
same && (first[i] == second[i])
in
same

def colorCode (color: []u8): i32 =
if equal color "black" then 0 else
if equal color "brown" then 1 else
if equal color "red" then 2 else
if equal color "orange" then 3 else
if equal color "yellow" then 4 else
if equal color "green" then 5 else
if equal color "blue" then 6 else
if equal color "violet" then 7 else
if equal color "grey" then 8 else
if equal color "white" then 9 else
assert false 0

def value (first: []u8) (second: []u8): i32 =
10 * (colorCode first) + (colorCode second)
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def value (first: []u8) (second: []u8): i32 = ???
39 changes: 39 additions & 0 deletions exercises/practice/resistor-color-duo/test.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "resistor_color_duo"

-- Brown and black
-- ==
-- input { "brown" "black" }
-- output { 10 }

-- Blue and grey
-- ==
-- input { "blue" "grey" }
-- output { 68 }

-- Yellow and violet
-- ==
-- input { "yellow" "violet" }
-- output { 47 }

-- White and red
-- ==
-- input { "white" "red" }
-- output { 92 }

-- Orange and orange
-- ==
-- input { "orange" "orange" }
-- output { 33 }

-- Ignore additional colors
-- ==
-- input { "green" "brown" }
-- output { 51 }

-- Black and brown, one-digit
-- ==
-- input { "black" "brown" }
-- output { 1 }

let main (first: []u8) (second: []u8): i32 =
value first second
11 changes: 11 additions & 0 deletions generators/exercises/resistor_color_duo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def gen_test_case(prop, description, inp, expected, f):
colors = inp["colors"]
f.write(f"-- {description}\n")
f.write("-- ==\n")
f.write(f'-- input {{ "{colors[0]}" "{colors[1]}" }}\n')
f.write(f"-- output {{ {expected} }}\n\n")


def gen_main(f):
f.write("let main (first: []u8) (second: []u8): i32 =\n")
f.write(" value first second\n")