Skip to content

implement exercise resistor-color #1630

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

Merged
merged 2 commits into from
Feb 14, 2019
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
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
"strings"
]
},
{
"slug": "resistor-color",
"uuid": "e5e821ed-fc1f-4419-9c90-ad4a0b564bea",
"core": false,
"unlocked_by": "two-fer",
"difficulty": 2,
"topics": [
"arrays",
"strings"
]
},
{
"slug": "darts",
"uuid": "4d400a44-b190-4a0c-affb-99fad8ea18da",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Arrays;

class ResistorColor {

final String[] COLORS = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};

int colorCode(String color) {
return Arrays.asList(COLORS).indexOf(color);
}

String[] colors() {
return COLORS;
}
}
2 changes: 2 additions & 0 deletions exercises/resistor-color/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1.0.0

38 changes: 38 additions & 0 deletions exercises/resistor-color/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Resistor Color

Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

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

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](https://en.wikipedia.org/wiki/Electronic_color_code)

# Running the tests

You can run all the tests for an exercise by entering

```sh
$ gradle test
```

in your terminal.

## Source

Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1458](https://github.com/exercism/problem-specifications/issues/1458)

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.
18 changes: 18 additions & 0 deletions exercises/resistor-color/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
9 changes: 9 additions & 0 deletions exercises/resistor-color/src/main/java/ResistorColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ResistorColor {
int colorCode(String color) {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

String[] colors() {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}
}
50 changes: 50 additions & 0 deletions exercises/resistor-color/src/test/java/ResistorColorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class ResistorColorTest {

private ResistorColor resistorColor;

@Before
public void setup() {
resistorColor = new ResistorColor();
}

@Test
public void testBlackColorCode() {
String input = "black";
int expected = 0;

assertEquals(expected, resistorColor.colorCode(input));
}

@Ignore("Remove to run test")
@Test
public void testWhiteColorCode() {
String input = "white";
int expected = 9;

assertEquals(expected, resistorColor.colorCode(input));
}

@Ignore("Remove to run test")
@Test
public void testOrangeColorCode() {
String input = "orange";
int expected = 3;

assertEquals(expected, resistorColor.colorCode(input));
}

@Ignore("Remove to run test")
@Test
public void testColors() {
String[] expected = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};

assertEquals(expected, resistorColor.colors());
}

}