Skip to content

Commit

Permalink
start resistor color duo
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Nov 26, 2023
1 parent c6b8a65 commit 05d91a9
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/resistor-color-duo/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [
"andrerfcsantos"
],
"files": {
"solution": [
"resistor_color_duo.go"
],
"test": [
"resistor_color_duo_test.go"
],
"example": [
".meta/example.go"
],
"editor": [
"cases_test.go"
],
"invalidator": [
"go.mod"
]
},
"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"
}
1 change: 1 addition & 0 deletions go/resistor-color-duo/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"go","exercise":"resistor-color-duo","id":"2953814d62b844868c50b93a81056840","url":"https://exercism.org/tracks/go/exercises/resistor-color-duo","handle":"rootulp","is_requester":true,"auto_approve":false}
41 changes: 41 additions & 0 deletions go/resistor-color-duo/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Help

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with
different specs, so the results from these benchmark tests may vary.

## Submitting your solution

You can submit your solution using the `exercism submit resistor_color_duo.go` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Go track's documentation](https://exercism.org/docs/tracks/go)
- The [Go track's programming category on the forum](https://forum.exercism.org/c/programming/go)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [How to Write Go Code](https://golang.org/doc/code.html)
- [Effective Go](https://golang.org/doc/effective_go.html)
- [Go Resources](http://golang.org/help)
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)
48 changes: 48 additions & 0 deletions go/resistor-color-duo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Resistor Color Duo

Welcome to Resistor Color Duo on Exercism's Go Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## 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.

## Source

### Created by

- @andrerfcsantos

### Based on

Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1464
51 changes: 51 additions & 0 deletions go/resistor-color-duo/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package resistorcolorduo

// This is an auto-generated file. Do not change it manually. Run the generator to update the file.
// See https://github.com/exercism/go#synchronizing-tests-and-instructions
// Source: exercism/problem-specifications
// Commit: 7b395a7 Resistor-color-duo: one-digit solution

type valueTestCase struct {
description string
input []string
expected int
}

var valueTestCases = []valueTestCase{

{
description: "Brown and black",
input: []string{"brown", "black"},
expected: 10,
},
{
description: "Blue and grey",
input: []string{"blue", "grey"},
expected: 68,
},
{
description: "Yellow and violet",
input: []string{"yellow", "violet"},
expected: 47,
},
{
description: "White and red",
input: []string{"white", "red"},
expected: 92,
},
{
description: "Orange and orange",
input: []string{"orange", "orange"},
expected: 33,
},
{
description: "Ignore additional colors",
input: []string{"green", "brown", "orange"},
expected: 51,
},
{
description: "Black and brown, one-digit",
input: []string{"black", "brown"},
expected: 1,
},
}
3 changes: 3 additions & 0 deletions go/resistor-color-duo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module resistorcolorduo

go 1.18
6 changes: 6 additions & 0 deletions go/resistor-color-duo/resistor_color_duo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package resistorcolorduo

// Value should return the resistance value of a resistor with a given colors.
func Value(colors []string) int {
panic("Implement the Value function")
}
26 changes: 26 additions & 0 deletions go/resistor-color-duo/resistor_color_duo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package resistorcolorduo

import "testing"

func TestValue(t *testing.T) {
for _, tc := range valueTestCases {
t.Run(tc.description, func(t *testing.T) {
actual := Value(tc.input)

if actual != tc.expected {
t.Fatalf("Value(%+q): expected %d, actual %d", tc.input, tc.expected, actual)
}
})
}
}

// valueBench is intended to be used in BenchmarkValue to avoid compiler optimizations.
var valueBench int

func BenchmarkValue(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range valueTestCases {
valueBench = Value(tc.input)
}
}
}

0 comments on commit 05d91a9

Please sign in to comment.