Skip to content
Closed
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
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@
"enumerations"
]
},
{
"slug": "resistor-color-trio",
"uuid": "080f18d8-fb29-44f5-882f-65e772cb5324",
"core": false,
"unlocked_by": "two-fer",
"difficulty": 3,
"topics": [
"arrays",
"strings",
"enumerations",
"math",
"conditionals"
]
},
{
"slug": "darts",
"uuid": "4d400a44-b190-4a0c-affb-99fad8ea18da",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ResistorColorTrio {
private enum Color {
BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GREY, WHITE;
};

String value(String[] colors) {
int intValue = (10 * Color.valueOf(colors[0]).ordinal() + Color.valueOf(colors[1]).ordinal())
* (int) Math.pow(10, Color.valueOf(colors[2]).ordinal());
String stringValue;
if (intValue >= 1000) {
stringValue = String.valueOf(intValue / 1000) + " kiloohms";
} else {
stringValue = String.valueOf(intValue) + " ohms";
}
return stringValue;
Comment on lines +7 to +15
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The canonical implementation should probably include encoding more information into the Color enum. Might also want to consider another enum for the powers (like OHMS, KILOOHMS, ...). This solution only adds support for kiloohms and we can do better (I know the Kotlin version goes up to something like exaohms).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Haskell solution only goes to giga, but adds optional edge cases that the original exercise does not address, because functional programmers have an obsession with totality. The point is that while you could extend the range beyond giga, you could also do a lot of other things, or not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suppose I am biased by having seen the Kotlin implementation of this exercise. IMO: we should go up to giga specifically since the exercise naturally produces gigaohms.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Keeping it simple and as specific as possible seemed like a good option.
However, as per my other comment, i do not know if this is preferrable or not.

}
}
1 change: 1 addition & 0 deletions exercises/resistor-color-trio/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
76 changes: 76 additions & 0 deletions exercises/resistor-color-trio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Resistor Color Trio

If you want to build something using a Raspberry Pi, you'll probably use _resistors_. For this exercise, you need to know only three 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 acts as a digit of a 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 3 colors as input, and outputs the correct value, in ohms.
The color bands are encoded as follows:

* Black: 0
* Brown: 1
* Red: 2
* Orange: 3
* Yellow: 4
* Green: 5
* Blue: 6
* Violet: 7
* Grey: 8
* White: 9

In `resistor-color duo` you decoded the first two colors. For instance: orange-orange got the main value `33`.
The third color stands for how many zeros need to be added to the main value. The main value plus the zeros gives us a value in ohms.
For the exercise it doesn't matter what ohms really are.
For example:

- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.

(If Math is your thing, you may want to think of the zeros as exponents of 10. If Math is not your thing, go with the zeros. It really is the same thing, just in plain English instead of Math lingo.)

This exercise is about translating the colors into a label:

> "... ohms"

So an input of `"orange", "orange", "black"` should return:

> "33 ohms"

When we get more than a thousand ohms, we say "kiloohms". That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.
So an input of `"orange", "orange", "orange"` should return:

> "33 kiloohms"

## Setup

Go through the setup instructions for Java to install the necessary
dependencies:

[https://exercism.io/tracks/java/installation](https://exercism.io/tracks/java/installation)

# Running the tests

You can run all the tests for an exercise by entering the following in your
terminal:

```sh
$ gradle test
```

> Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the
`@Ignore("Remove to run test")` annotation.

## Source

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

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

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'short'
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ResistorColorTrio {
String value(String[] colors) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
String value(String[] colors) {
String value(Color... colors) {

This should really take the Color enum as input. It's a good setup for varargs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From the exercise I thought the idea behind the exercise was applying knowledge about enums. Thus predefining them might defeat the purpose.

Fairly honestly however, I have since almost fully forgotten how exercism works and/or why the entire project is set up like it is. My apologies.

throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class ResistorColorTrioTest {
private ResistorColorTrio resistorColorTrio = new ResistorColorTrio();

@Test
public void testOrangeOrangeBlack() {
String[] input = { "orange", "orange", "black" };
String expected = "33 ohms";
String actual = resistorColorTrio.value(input);
Comment on lines +12 to +14
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
String[] input = { "orange", "orange", "black" };
String expected = "33 ohms";
String actual = resistorColorTrio.value(input);
String expected = "33 ohms";
String actual = resistorColorTrio.value(Color.ORANGE, Color.ORANGE, Color.BLACK);

Same throughout.


assertEquals(expected, actual);
}

@Test
@Ignore("Remove to run test")
public void testBlueGreyBrown() {
String[] input = { "blue", "grey", "brown" };
String expected = "680 ohms";
String actual = resistorColorTrio.value(input);

assertEquals(expected, actual);
}

@Test
@Ignore("Remove to run test")
public void testRedBlackRed() {
String[] input = { "red", "black", "red" };
String expected = "2 kiloohms";
String actual = resistorColorTrio.value(input);

assertEquals(expected, actual);
}

@Test
@Ignore("Remove to run test")
public void testGreenBrownOrange() {
String[] input = { "green", "brown", "orange" };
String expected = "51 kiloohms";
String actual = resistorColorTrio.value(input);

assertEquals(expected, actual);
}

@Test
@Ignore("Remove to run test")
public void testYellowVioletYellow() {
String[] input = { "yellow", "violet", "yellow" };
String expected = "470 kiloohms";
String actual = resistorColorTrio.value(input);

assertEquals(expected, actual);
}
}
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ include 'rational-numbers'
include 'rectangles'
include 'resistor-color'
include 'resistor-color-duo'
include 'resistor-color-trio'
include 'reverse-string'
include 'rna-transcription'
include 'robot-name'
Expand Down