-
-
Notifications
You must be signed in to change notification settings - Fork 759
resistor-color-trio: create exercise #1772
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
Changes from all commits
4bc15bf
dffba74
fd0faa1
7752b91
5149cd2
8b0e461
6f0089d
b0ea2ce
da5fe12
417065c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1.0.0 |
| 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. |
| 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) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This should really take the Color enum as input. It's a good setup for varargs.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.