Explorar el Código

Added exercise raindrops

Piet van Dongen hace 11 años
padre
commit
0ed5f07fcf

+ 2
- 1
config.json Ver fichero

@@ -20,7 +20,8 @@
20 20
     "scrabble-score",
21 21
     "roman-numerals",
22 22
     "binary",
23
-    "prime-factors"
23
+    "prime-factors",
24
+    "raindrops"
24 25
   ],
25 26
   "deprecated": [
26 27
   ],

+ 11
- 0
raindrops/build.gradle Ver fichero

@@ -0,0 +1,11 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.10"
11
+}

+ 24
- 0
raindrops/example.java Ver fichero

@@ -0,0 +1,24 @@
1
+public class Raindrops {
2
+
3
+    public static String convert(int number) {
4
+        String result = "";
5
+
6
+        if (number % 3 == 0) {
7
+            result += "Pling";
8
+        }
9
+
10
+        if (number % 5 == 0) {
11
+            result += "Plang";
12
+        }
13
+
14
+        if (number % 7 == 0) {
15
+            result += "Plong";
16
+        }
17
+
18
+        if (result == null || result.isEmpty()) {
19
+            result = Integer.toString(number);
20
+        }
21
+
22
+        return result;
23
+    }
24
+}

+ 0
- 0
raindrops/src/main/java/.keep Ver fichero


+ 2
- 0
raindrops/src/main/java/Raindrops.java Ver fichero

@@ -0,0 +1,2 @@
1
+public class Raindrops {
2
+}

+ 57
- 0
raindrops/src/test/java/RaindropsTest.java Ver fichero

@@ -0,0 +1,57 @@
1
+import org.junit.Test;
2
+import org.junit.runner.RunWith;
3
+import org.junit.runners.Parameterized;
4
+import org.junit.runners.Parameterized.Parameters;
5
+
6
+import java.util.Arrays;
7
+import java.util.Collection;
8
+
9
+import static org.junit.Assert.assertEquals;
10
+
11
+@RunWith(Parameterized.class)
12
+public class RaindropsTest {
13
+
14
+    private int input;
15
+    private String expectedOutput;
16
+
17
+    @Parameters
18
+    public static Collection<Object[]> data() {
19
+        return Arrays.asList(new Object[][]{
20
+                // Non-primes
21
+                {1, "1"},
22
+                {52, "52"},
23
+                {12121, "12121"},
24
+
25
+                // Numbers with 3 as a prime factor
26
+                {3, "Pling"},
27
+                {6, "Pling"},
28
+                {9, "Pling"},
29
+
30
+                // Numbers with 5 as a prime factor
31
+                {5, "Plang"},
32
+                {10, "Plang"},
33
+                {25, "Plang"},
34
+
35
+                // Numbers with 7 as a prime factor
36
+                {7, "Plong"},
37
+                {14, "Plong"},
38
+                {49, "Plong"},
39
+
40
+                // Numbers with multiple activating prime factors
41
+                {15, "PlingPlang"},
42
+                {21, "PlingPlong"},
43
+                {35, "PlangPlong"},
44
+                {105, "PlingPlangPlong"},
45
+        });
46
+    }
47
+
48
+    public RaindropsTest(int input, String expectedOutput) {
49
+        this.input = input;
50
+        this.expectedOutput = expectedOutput;
51
+    }
52
+
53
+    @Test
54
+    public void test() {
55
+        assertEquals(expectedOutput, Raindrops.convert(input));
56
+    }
57
+}