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 @@ -384,6 +384,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "resistor-color",
"name": "Resistor Color",
"uuid": "2043f397-4337-4d3f-867e-50e344b790eb",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "darts",
"name": "Darts",
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/resistor-color/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.

These 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

The goal of this exercise is to create a way:

- to look up the numerical value associated with a particular color band
- to list the different band colors

Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array:
Better Be Right Or Your Great Big Values Go Wrong.

More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code].

[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation

enum Color: String, CaseIterable {
case black
case brown
case red
case orange
case yellow
case green
case blue
case violet
case grey
case white
}

enum ResistorColorError: Error {
case unknownColor
}

enum ResistorColor {

static func colorCode(for name: String) throws -> Int {
guard let color = Color(rawValue: name) else {
throw ResistorColorError.unknownColor
}
return code(for: color)
}

static var colors: [String] {
return Array(Color.allCases.map { $0.rawValue })
}

}

fileprivate func code(for color: Color) -> Int {
switch color {
case .black: return 0
case .brown: return 1
case .red: return 2
case .orange: return 3
case .yellow: return 4
case .green: return 5
case .blue: return 6
case .violet: return 7
case .grey: return 8
case .white: return 9
}
}
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"Sencudra"
],
"files": {
"solution": [
"Sources/ResistorColor/ResistorColor.swift"
],
"test": [
"Tests/ResistorColorTests/ResistorColorTests.swift"
],
"example": [
".meta/Sources/ResistorColor/ResistorColorExample.swift"
]
},
"blurb": "Convert a resistor band's color to its numeric representation.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
}
32 changes: 32 additions & 0 deletions exercises/practice/resistor-color/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Testing
import Foundation
@testable import {{ exercise | camelCase }}

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct {{ exercise | camelCase }}Tests {

{% for case in cases.first.cases %}
{% if forloop.first -%}
@Test("{{case.description}}")
{% else -%}
@Test("{{case.description}}", .enabled(if: RUNALL))
{% endif -%}
func test{{ case.description | camelCase }}() throws {
#expect(try ResistorColor.{{ case.property }}(for: "{{ case.input.color }}") == {{ case.expected }})
}
{% endfor -%}

{% for case in cases %}
{% if forloop.first -%}
{% continue %}
{% endif %}
@Test("{{case.description}}", .enabled(if: RUNALL))
func test{{ case.description | camelCase }}() {
{% if case.property == "colors" -%}
#expect(ResistorColor.{{ case.property }} == {{ case.expected | toStringArray }})
{% endif -%}
}
{% endfor -%}

}
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

[49eb31c5-10a8-4180-9f7f-fea632ab87ef]
description = "Color codes -> Black"

[0a4df94b-92da-4579-a907-65040ce0b3fc]
description = "Color codes -> White"

[5f81608d-f36f-4190-8084-f45116b6f380]
description = "Color codes -> Orange"

[581d68fa-f968-4be2-9f9d-880f2fb73cf7]
description = "Colors"
21 changes: 21 additions & 0 deletions exercises/practice/resistor-color/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "ResistorColor",
products: [
.library(
name: "ResistorColor",
targets: ["ResistorColor"])
],
dependencies: [],
targets: [
.target(
name: "ResistorColor",
dependencies: []),
.testTarget(
name: "ResistorColorTests",
dependencies: ["ResistorColor"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

enum ResistorColor {
// Write your code for the 'ResistorColor' exercise in this file.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import Testing

@testable import ResistorColor

let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct ResistorColorTests {

@Test("Black")
func testBlack() throws {
#expect(try ResistorColor.colorCode(for: "black") == 0)
}

@Test("White", .enabled(if: RUNALL))
func testWhite() throws {
#expect(try ResistorColor.colorCode(for: "white") == 9)
}

@Test("Orange", .enabled(if: RUNALL))
func testOrange() throws {
#expect(try ResistorColor.colorCode(for: "orange") == 3)
}

@Test("Colors", .enabled(if: RUNALL))
func testColors() {
#expect(
ResistorColor.colors == [
"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white",
])
}
}