Skip to content

Commit 5a11664

Browse files
Add all three resistor exercises (#9)
* All three resistor exercises * Difficulties
1 parent b569e60 commit 5a11664

File tree

19 files changed

+388
-0
lines changed

19 files changed

+388
-0
lines changed

config.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@
6969
"practices": [],
7070
"prerequisites": [],
7171
"difficulty": 1
72+
},
73+
{
74+
"slug": "resistor-color-duo",
75+
"name": "Resistor Color Duo",
76+
"uuid": "e4c59681-3664-4ad4-8948-83b6084ad244",
77+
"practices": [],
78+
"prerequisites": [],
79+
"difficulty": 1
80+
},
81+
{
82+
"slug": "resistor-color",
83+
"name": "Resistor Color",
84+
"uuid": "366f8b1c-2368-4e96-9dff-21b552a3be14",
85+
"practices": [],
86+
"prerequisites": [],
87+
"difficulty": 1
88+
},
89+
{
90+
"slug": "resistor-color-trio",
91+
"name": "Resistor Color Trio",
92+
"uuid": "a1f5a88f-aa72-405f-88a3-e1a120ce3a89",
93+
"practices": [],
94+
"prerequisites": [],
95+
"difficulty": 1
7296
}
7397
]
7498
},
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
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.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- black: 0
21+
- brown: 1
22+
- red: 2
23+
- orange: 3
24+
- yellow: 4
25+
- green: 5
26+
- blue: 6
27+
- violet: 7
28+
- grey: 8
29+
- white: 9
30+
31+
From the example above:
32+
brown-green should return 15, and
33+
brown-green-violet should return 15 too, ignoring the third color.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"quintuple-mallard"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor-color-duo.nu"
8+
],
9+
"test": [
10+
"tests.nu"
11+
],
12+
"example": [
13+
".meta/example.nu"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464",
19+
"difficulty": 2
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def color_code [color] {
2+
[black brown red orange yellow green blue violet grey white]
3+
| enumerate
4+
| filter {|item| $item.item == $color}
5+
| get index
6+
| get 0
7+
}
8+
export def value [colors] {
9+
(color_code $colors.0) * 10 + (color_code $colors.1)
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
13+
description = "Brown and black"
14+
15+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
16+
description = "Blue and grey"
17+
18+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
19+
description = "Yellow and violet"
20+
21+
[b7a6cbd2-ae3c-470a-93eb-56670b305640]
22+
description = "White and red"
23+
24+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
25+
description = "Orange and orange"
26+
27+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
28+
description = "Ignore additional colors"
29+
30+
[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
31+
description = "Black and brown, one-digit"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export def value [colors] {
2+
error make {"msg": "Remove this line and implement value"}
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use resistor-color-duo.nu value
2+
use std/assert
3+
assert equal (value [brown black]) 10
4+
assert equal (value [blue grey]) 68
5+
assert equal (value [yellow violet]) 47
6+
assert equal (value [orange orange]) 33
7+
assert equal (value [green brown orange]) 51
8+
assert equal (value [black brown]) 1
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know only three things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
9+
- Each band acts as a digit of a number.
10+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
11+
In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands.
12+
The program will take 3 colors as input, and outputs the correct value, in ohms.
13+
The color bands are encoded as follows:
14+
15+
- black: 0
16+
- brown: 1
17+
- red: 2
18+
- orange: 3
19+
- yellow: 4
20+
- green: 5
21+
- blue: 6
22+
- violet: 7
23+
- grey: 8
24+
- white: 9
25+
26+
In Resistor Color Duo you decoded the first two colors.
27+
For instance: orange-orange got the main value `33`.
28+
The third color stands for how many zeros need to be added to the main value.
29+
The main value plus the zeros gives us a value in ohms.
30+
For the exercise it doesn't matter what ohms really are.
31+
For example:
32+
33+
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
34+
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
35+
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
36+
37+
(If Math is your thing, you may want to think of the zeros as exponents of 10.
38+
If Math is not your thing, go with the zeros.
39+
It really is the same thing, just in plain English instead of Math lingo.)
40+
41+
This exercise is about translating the colors into a label:
42+
43+
> "... ohms"
44+
45+
So an input of `"orange", "orange", "black"` should return:
46+
47+
> "33 ohms"
48+
49+
When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
50+
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".
51+
52+
For example, an input of `"orange", "orange", "orange"` should return:
53+
54+
> "33 kiloohms"
55+
56+
[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"quintuple-mallard"
4+
],
5+
"files": {
6+
"solution": [
7+
"resistor-color-trio.nu"
8+
],
9+
"test": [
10+
"tests.nu"
11+
],
12+
"example": [
13+
".meta/example.nu"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1549",
19+
"difficulty": 4
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def color_code [color] {
2+
[black brown red orange yellow green blue violet grey white]
3+
| enumerate
4+
| filter {|item| $item.item == $color}
5+
| get index
6+
| get 0
7+
}
8+
def value [colors] {
9+
(color_code $colors.0) * 10 + (color_code $colors.1)
10+
}
11+
12+
def unit [] {
13+
let ohms = $in
14+
if $ohms < 1_000 {
15+
$"($ohms) ohms"
16+
} else if $ohms < 1_000_000 {
17+
$"($ohms / 1_000) kiloohms"
18+
} else if $ohms < 1_000_000_000 {
19+
$"($ohms / 1_000_000) megaohms"
20+
} else {
21+
$"($ohms / 1_000_000_000) gigaohms"
22+
}
23+
}
24+
export def label [colors] {
25+
(value $colors) * 10 ** ((color_code $colors.2)) | unit
26+
}

0 commit comments

Comments
 (0)