🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of LukaszDembicki

LukaszDembicki's solution

to Resistor Color Duo in the Java Track

Published at Feb 28 2021 · 0 comments
Instructions
Test suite
Solution

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

Setup

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

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:

$ gradle test

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/1464

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ResistorColorDuoTest.java

import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class ResistorColorDuoTest {
    private ResistorColorDuo resistorColorDuo;

    @Before
    public void setup() {
        resistorColorDuo = new ResistorColorDuo();
    }

    @Test
    public void testBrownAndBlack() {
        String[] input = { "brown", "black" };
        int expected = 10;
        int actual = resistorColorDuo.value(input);

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void testBlueAndGrey() {
        String[] input = { "blue", "grey" };
        int expected = 68;
        int actual = resistorColorDuo.value(input);

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void testYellowAndViolet() {
        String[] input = { "yellow", "violet" };
        int expected = 47;
        int actual = resistorColorDuo.value(input);

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void testOrangeAndOrange() {
        String[] input = { "orange", "orange" };
        int expected = 33;
        int actual = resistorColorDuo.value(input);

        assertEquals(expected, actual);
    }

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

        assertEquals(expected, actual);
    }
}
class ResistorColorDuo {
    int value(String[] colors) {
        String totalResistance = "";
        for (int i = 0; i < colors.length; i++) {
            totalResistance = totalResistance.concat(String.valueOf(this.colorCode(colors[i])));
            if (i >= 1) {
                break;
            }
        }

        if (totalResistance.isEmpty()) {
            return 0;
        }

        return Integer.parseInt(totalResistance);
    }

    int colorCode(String color) {
        String[] allColors = this.colors();
        for (int i = 0; i <= allColors.length - 1; i++) {
            if (allColors[i].equals(color)) {
                return i;
            }
        }

        return 0;
    }

    String[] colors() {
        String[] expected = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
        return expected;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?