diff --git a/config.json b/config.json index 658f34d51..32bb99ef3 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/resistor-color-trio/.meta/src/reference/java/ResistorColorTrio.java b/exercises/resistor-color-trio/.meta/src/reference/java/ResistorColorTrio.java new file mode 100644 index 000000000..af72c2e85 --- /dev/null +++ b/exercises/resistor-color-trio/.meta/src/reference/java/ResistorColorTrio.java @@ -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; + } +} diff --git a/exercises/resistor-color-trio/.meta/version b/exercises/resistor-color-trio/.meta/version new file mode 100644 index 000000000..3eefcb9dd --- /dev/null +++ b/exercises/resistor-color-trio/.meta/version @@ -0,0 +1 @@ +1.0.0 diff --git a/exercises/resistor-color-trio/README.md b/exercises/resistor-color-trio/README.md new file mode 100644 index 000000000..4109b80e7 --- /dev/null +++ b/exercises/resistor-color-trio/README.md @@ -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. diff --git a/exercises/resistor-color-trio/build.gradle b/exercises/resistor-color-trio/build.gradle new file mode 100644 index 000000000..82d705dfd --- /dev/null +++ b/exercises/resistor-color-trio/build.gradle @@ -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"] + } +} diff --git a/exercises/resistor-color-trio/src/main/java/ResistorColorTrio.java b/exercises/resistor-color-trio/src/main/java/ResistorColorTrio.java new file mode 100644 index 000000000..3331db205 --- /dev/null +++ b/exercises/resistor-color-trio/src/main/java/ResistorColorTrio.java @@ -0,0 +1,5 @@ +class ResistorColorTrio { + String value(String[] colors) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } +} diff --git a/exercises/resistor-color-trio/src/test/java/ResistorColorTrioTest.java b/exercises/resistor-color-trio/src/test/java/ResistorColorTrioTest.java new file mode 100644 index 000000000..68ab63d4f --- /dev/null +++ b/exercises/resistor-color-trio/src/test/java/ResistorColorTrioTest.java @@ -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); + + 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); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index cdd2bfbde..ff822cab5 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -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'