Просмотр исходного кода

Merge pull request #616 from exercism/collatz-conjecture

collatz-conjecture: add to track
FridaTveit 9 лет назад
Родитель
Сommit
6088fb0737

+ 5
- 0
config.json Просмотреть файл

@@ -96,6 +96,11 @@
96 96
       "topics": []
97 97
     },
98 98
     {
99
+      "slug": "collatz-conjecture",
100
+      "difficulty": 4,
101
+      "topics": []
102
+    },
103
+    {
99 104
       "slug": "nth-prime",
100 105
       "difficulty": 4,
101 106
       "topics": []

+ 18
- 0
exercises/collatz-conjecture/build.gradle Просмотреть файл

@@ -0,0 +1,18 @@
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.12"
11
+}
12
+
13
+test {
14
+    testLogging {
15
+        exceptionFormat = 'full'
16
+        events = ["passed", "failed", "skipped"]
17
+    }
18
+}

+ 19
- 0
exercises/collatz-conjecture/src/example/java/CollatzCalculator.java Просмотреть файл

@@ -0,0 +1,19 @@
1
+class CollatzCalculator {
2
+
3
+    int computeStepCount(final int start) {
4
+        if (start <= 0) throw new IllegalArgumentException("Only natural numbers are allowed");
5
+
6
+        if (start == 1) return 0;
7
+
8
+        final int next;
9
+
10
+        if (start % 2 == 0) {
11
+            next = start / 2;
12
+        } else {
13
+            next = 3 * start + 1;
14
+        }
15
+
16
+        return 1 + computeStepCount(next);
17
+    }
18
+
19
+}

+ 7
- 0
exercises/collatz-conjecture/src/main/java/CollatzCalculator.java Просмотреть файл

@@ -0,0 +1,7 @@
1
+class CollatzCalculator {
2
+
3
+    int computeStepCount(int start) {
4
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5
+    }
6
+
7
+}

+ 59
- 0
exercises/collatz-conjecture/src/test/java/CollatzCalculatorTest.java Просмотреть файл

@@ -0,0 +1,59 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Rule;
4
+import org.junit.Test;
5
+import org.junit.rules.ExpectedException;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+
9
+/*
10
+ * version: 1.0.0
11
+ */
12
+public class CollatzCalculatorTest {
13
+
14
+    @Rule
15
+    public ExpectedException expectedException = ExpectedException.none();
16
+
17
+    private CollatzCalculator collatzCalculator;
18
+
19
+    @Before
20
+    public void setUp() {
21
+        collatzCalculator = new CollatzCalculator();
22
+    }
23
+
24
+    @Test
25
+    public void testZeroStepsRequiredWhenStartingFrom1() {
26
+        assertEquals(0, collatzCalculator.computeStepCount(1));
27
+    }
28
+
29
+    @Ignore("Remove to run test")
30
+    @Test
31
+    public void testCorrectNumberOfStepsWhenAllStepsAreDivisions() {
32
+        assertEquals(4, collatzCalculator.computeStepCount(16));
33
+    }
34
+
35
+    @Ignore("Remove to run test")
36
+    @Test
37
+    public void testCorrectNumberOfStepsWhenBothStepTypesAreNeeded() {
38
+        assertEquals(9, collatzCalculator.computeStepCount(12));
39
+    }
40
+
41
+    @Ignore("Remove to run test")
42
+    @Test
43
+    public void testZeroIsConsideredInvalidInput() {
44
+        expectedException.expect(IllegalArgumentException.class);
45
+        expectedException.expectMessage("Only natural numbers are allowed");
46
+
47
+        collatzCalculator.computeStepCount(0);
48
+    }
49
+
50
+    @Ignore("Remove to run test")
51
+    @Test
52
+    public void testNegativeIntegerIsConsideredInvalidInput() {
53
+        expectedException.expect(IllegalArgumentException.class);
54
+        expectedException.expectMessage("Only natural numbers are allowed");
55
+
56
+        collatzCalculator.computeStepCount(-15);
57
+    }
58
+
59
+}

+ 0
- 0
exercises/difference-of-squares/src/main/java/.keep Просмотреть файл


+ 0
- 15
exercises/difference-of-squares/src/main/java/DifferenceOfSquaresCalculator.java Просмотреть файл

@@ -1,15 +0,0 @@
1
-final class DifferenceOfSquaresCalculator {
2
-
3
-    int computeSquareOfSumTo(final int input) {
4
-        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5
-    }
6
-
7
-    int computeSumOfSquaresTo(final int input) {
8
-        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9
-    }
10
-
11
-    int computeDifferenceOfSquares(final int input) {
12
-      throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
13
-    }
14
-
15
-}

+ 1
- 0
exercises/settings.gradle Просмотреть файл

@@ -13,6 +13,7 @@ include 'book-store'
13 13
 include 'bracket-push'
14 14
 include 'change'
15 15
 include 'clock'
16
+include 'collatz-conjecture'
16 17
 include 'crypto-square'
17 18
 include 'custom-set'
18 19
 include 'diamond'