Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resistor-color-duo #703

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -691,6 +691,14 @@
"unicode"
]
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "c71c65c4-0572-4dd6-b864-317486821e54",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "resistor-color-trio",
"name": "Resistor Color Trio",
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
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": [
"BNAndras"
],
"files": {
"solution": [
"resistor-color-duo.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"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"
}
23 changes: 23 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const COLORS = [
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white"
]

function colorcode(colors)
tens = value(colors[1])
ones = value(colors[2])

tens * 10 + ones
end

function value(color)
findfirst(==(color), COLORS) - 1
end
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"
3 changes: 3 additions & 0 deletions exercises/practice/resistor-color-duo/resistor-color-duo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function colorcode(colors)

end
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Test

include("resistor-color-duo.jl")

@testset verbose = true "Resistor Color Duo" begin
@testset "Brown and black" begin
@test colorcode(["brown", "black"]) == 10
Copy link
Contributor

Choose a reason for hiding this comment

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

It's still weird that this and the trio exercise pass in a vector rather than separate arguments. Is this related to the learning objectives for the exercise?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I know, that was by design. Notably, Haskell does separate arguments (see https://github.com/exercism/haskell/blob/main/exercises/practice/resistor-color-duo/src/ResistorColors.hs and https://github.com/exercism/haskell/blob/main/exercises/practice/resistor-color-trio/src/ResistorColors.hs). However since we already use a vector in resistor-color-trio, it makes sense to do the same here for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, maybe it'll help people who haven't worked out how to index collections yet or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should resistor-color-trio be updated then? You'll break solutions, but the whole resistor color series will be consistent on how they handle arguments.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I don't think so. I was saying "sure let's do the same as trio for consistency".

end

@testset "Blue and grey" begin
@test colorcode(["blue", "grey"]) == 68
end

@testset "Yellow and violet" begin
@test colorcode(["yellow", "violet"]) == 47
end

@testset "White and red" begin
@test colorcode(["white", "red"]) == 92
end

@testset "Orange and orange" begin
@test colorcode(["orange", "orange"]) == 33
end

@testset "Ignore additional colors" begin
@test colorcode(["green", "brown", "orange"]) == 51
end

@testset "Black and brown, one digit" begin
@test colorcode(["black", "brown"]) == 1
end
end
Loading