Skip to content

Commit 65abe5f

Browse files
authored
Add resistor-color-duo (#862)
1 parent d7639b8 commit 65abe5f

File tree

9 files changed

+228
-0
lines changed

9 files changed

+228
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,14 @@
520520
"text_formatting"
521521
]
522522
},
523+
{
524+
"slug": "resistor-color-duo",
525+
"name": "Resistor Color Duo",
526+
"uuid": "e6b8cbce-c75b-492c-aca4-2a078837bb9d",
527+
"practices": [],
528+
"prerequisites": [],
529+
"difficulty": 2
530+
},
523531
{
524532
"slug": "binary-search",
525533
"name": "Binary Search",
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Foundation
2+
3+
enum Color: String, CaseIterable {
4+
case black
5+
case brown
6+
case red
7+
case orange
8+
case yellow
9+
case green
10+
case blue
11+
case violet
12+
case grey
13+
case white
14+
}
15+
16+
enum ResistorColorDuoError: Error {
17+
case unknownColor
18+
}
19+
20+
enum ResistorColorDuo {
21+
22+
static func value(for colors: [String]) throws -> Int {
23+
var result = 0
24+
for index in 0..<min(colors.count, 2) {
25+
guard let color = Color(rawValue: colors[index]) else {
26+
throw ResistorColorDuoError.unknownColor
27+
}
28+
let value = code(for: color)
29+
result = result * 10 + value
30+
}
31+
return result
32+
}
33+
34+
}
35+
36+
fileprivate func code(for color: Color) -> Int {
37+
switch color {
38+
case .black: return 0
39+
case .brown: return 1
40+
case .red: return 2
41+
case .orange: return 3
42+
case .yellow: return 4
43+
case .green: return 5
44+
case .blue: return 6
45+
case .violet: return 7
46+
case .grey: return 8
47+
case .white: return 9
48+
}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"Sencudra"
4+
],
5+
"files": {
6+
"solution": [
7+
"Sources/ResistorColorDuo/ResistorColorDuo.swift"
8+
],
9+
"test": [
10+
"Tests/ResistorColorDuoTests/ResistorColorDuoTests.swift"
11+
],
12+
"example": [
13+
".meta/Sources/ResistorColorDuo/ResistorColorDuoExample.swift"
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Testing
2+
import Foundation
3+
@testable import {{ exercise | camelCase }}
4+
5+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
7+
@Suite struct {{ exercise | camelCase }}Tests {
8+
{% for case in cases %}
9+
{% if forloop.first -%}
10+
@Test("{{case.description}}")
11+
{% else -%}
12+
@Test("{{case.description}}", .enabled(if: RUNALL))
13+
{% endif -%}
14+
func test{{ case.description | camelCase }}() throws {
15+
#expect(try ResistorColorDuo.{{ case.property }}(for: {{ case.input.colors | toStringArray }}) == {{ case.expected }})
16+
}
17+
{% endfor -%}
18+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "ResistorColorDuo",
7+
products: [
8+
.library(
9+
name: "ResistorColorDuo",
10+
targets: ["ResistorColorDuo"])
11+
],
12+
dependencies: [],
13+
targets: [
14+
.target(
15+
name: "ResistorColorDuo",
16+
dependencies: []),
17+
.testTarget(
18+
name: "ResistorColorDuoTests",
19+
dependencies: ["ResistorColorDuo"]),
20+
]
21+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Foundation
2+
3+
enum ResistorColorDuo {
4+
// Write your code for the 'ResistorColorDuo' exercise in this file.
5+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
import Testing
3+
4+
@testable import ResistorColorDuo
5+
6+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
7+
8+
@Suite struct ResistorColorDuoTests {
9+
10+
@Test("Brown and black")
11+
func testBrownAndBlack() throws {
12+
#expect(try ResistorColorDuo.value(for: ["brown", "black"]) == 10)
13+
}
14+
15+
@Test("Blue and grey", .enabled(if: RUNALL))
16+
func testBlueAndGrey() throws {
17+
#expect(try ResistorColorDuo.value(for: ["blue", "grey"]) == 68)
18+
}
19+
20+
@Test("Yellow and violet", .enabled(if: RUNALL))
21+
func testYellowAndViolet() throws {
22+
#expect(try ResistorColorDuo.value(for: ["yellow", "violet"]) == 47)
23+
}
24+
25+
@Test("White and red", .enabled(if: RUNALL))
26+
func testWhiteAndRed() throws {
27+
#expect(try ResistorColorDuo.value(for: ["white", "red"]) == 92)
28+
}
29+
30+
@Test("Orange and orange", .enabled(if: RUNALL))
31+
func testOrangeAndOrange() throws {
32+
#expect(try ResistorColorDuo.value(for: ["orange", "orange"]) == 33)
33+
}
34+
35+
@Test("Ignore additional colors", .enabled(if: RUNALL))
36+
func testIgnoreAdditionalColors() throws {
37+
#expect(try ResistorColorDuo.value(for: ["green", "brown", "orange"]) == 51)
38+
}
39+
40+
@Test("Black and brown, one-digit", .enabled(if: RUNALL))
41+
func testBlackAndBrownOneDigit() throws {
42+
#expect(try ResistorColorDuo.value(for: ["black", "brown"]) == 1)
43+
}
44+
}

0 commit comments

Comments
 (0)