Avatar of artemkorsakov

artemkorsakov's solution

to Raindrops in the Java Track

Published at Jan 29 2019 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

RaindropConverterTest.java

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

import static org.junit.Assert.assertEquals;

public class RaindropConverterTest {

    private RaindropConverter raindropConverter = new RaindropConverter();

    @Test
    public void soundFor1Is1() {
        assertEquals("1", raindropConverter.convert(1));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor3IsPling() {
        assertEquals("Pling", raindropConverter.convert(3));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor5IsPlang() {
        assertEquals("Plang", raindropConverter.convert(5));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor7IsPlong() {
        assertEquals("Plong", raindropConverter.convert(7));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor6IsPlingAsItHasFactor3() {
        assertEquals("Pling", raindropConverter.convert(6));
    }

    @Ignore("Remove to run test")
    @Test
    public void noSoundFor2Cubed() {
        assertEquals("8", raindropConverter.convert(8));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor9IsPlingAsItHasFactor3() {
        assertEquals("Pling", raindropConverter.convert(9));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor10IsPlangAsItHasFactor5() {
        assertEquals("Plang", raindropConverter.convert(10));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor14IsPlongAsItHasFactor7() {
        assertEquals("Plong", raindropConverter.convert(14));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor15IsPlingPlangAsItHasFactors3And5() {
        assertEquals("PlingPlang", raindropConverter.convert(15));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor21IsPlingPlongAsItHasFactors3And7() {
        assertEquals("PlingPlong", raindropConverter.convert(21));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor25IsPlangAsItHasFactor5() {
        assertEquals("Plang", raindropConverter.convert(25));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor27IsPlingAsItHasFactor3() {
        assertEquals("Pling", raindropConverter.convert(27));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor35IsPlangPlongAsItHasFactors5And7() {
        assertEquals("PlangPlong", raindropConverter.convert(35));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor49IsPlongAsItHasFactor7() {
        assertEquals("Plong", raindropConverter.convert(49));
    }

    @Ignore("Remove to run test")
    @Test
    public void noSoundFor52() {
        assertEquals("52", raindropConverter.convert(52));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor105IsPlingPlangPlongAsItHasFactors3And5And7() {
        assertEquals("PlingPlangPlong", raindropConverter.convert(105));
    }

    @Ignore("Remove to run test")
    @Test
    public void soundFor3125IsPlangAsItHasFactor5() {
        assertEquals("Plang", raindropConverter.convert(3125));
    }

}
class RaindropConverter {

    String convert(int number) {
        StringBuilder sb = new StringBuilder();
        if (number % 3 == 0) {
            sb.append("Pling");
        }
        if (number % 5 == 0) {
            sb.append("Plang");
        }
        if (number % 7 == 0) {
            sb.append("Plong");
        }
        return sb.toString().isEmpty() ? "" + number : sb.toString();
    }

}

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?