Преглед изворни кода

perfect-numbers: add to track

Stuart Kent пре 9 година
родитељ
комит
2d1da4c399

+ 7
- 1
config.json Прегледај датотеку

@@ -61,7 +61,8 @@
61 61
     "clock",
62 62
     "diamond",
63 63
     "secret-handshake",
64
-    "flatten-array"
64
+    "flatten-array",
65
+    "perfect-numbers"
65 66
   ],
66 67
   "exercises": [
67 68
     {
@@ -353,6 +354,11 @@
353 354
       "slug": "flatten-array",
354 355
       "difficulty": 1,
355 356
       "topics": []
357
+    },
358
+    {
359
+      "slug": "perfect-numbers",
360
+      "difficulty": 1,
361
+      "topics": []
356 362
     }
357 363
   ],
358 364
   "deprecated": [

+ 17
- 0
exercises/perfect-numbers/build.gradle Прегледај датотеку

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

+ 1
- 0
exercises/perfect-numbers/src/example/java/Classification.java Прегледај датотеку

@@ -0,0 +1 @@
1
+../../main/java/Classification.java

+ 30
- 0
exercises/perfect-numbers/src/example/java/NaturalNumber.java Прегледај датотеку

@@ -0,0 +1,30 @@
1
+import java.util.stream.IntStream;
2
+
3
+final class NaturalNumber {
4
+
5
+    private final int naturalNumber;
6
+
7
+    NaturalNumber(int naturalNumber) {
8
+        if (naturalNumber <= 0) throw new IllegalStateException("Natural numbers are all positive.");
9
+        this.naturalNumber = naturalNumber;
10
+    }
11
+
12
+    Classification getClassification() {
13
+        final int aliquotSum = computeAliquotSum();
14
+
15
+        if (aliquotSum == naturalNumber) {
16
+            return Classification.PERFECT;
17
+        } else if (aliquotSum > naturalNumber) {
18
+            return Classification.ABUNDANT;
19
+        } else {
20
+            return Classification.DEFICIENT;
21
+        }
22
+    }
23
+
24
+    private int computeAliquotSum() {
25
+        return IntStream.range(1, naturalNumber)
26
+                .filter(it -> naturalNumber % it == 0)
27
+                .sum();
28
+    }
29
+
30
+}

+ 5
- 0
exercises/perfect-numbers/src/main/java/Classification.java Прегледај датотеку

@@ -0,0 +1,5 @@
1
+enum Classification {
2
+
3
+    DEFICIENT, PERFECT, ABUNDANT
4
+
5
+}

+ 5
- 0
exercises/perfect-numbers/src/main/java/NaturalNumber.java Прегледај датотеку

@@ -0,0 +1,5 @@
1
+final class NaturalNumber {
2
+
3
+
4
+
5
+}

+ 61
- 0
exercises/perfect-numbers/src/test/java/NaturalNumberTest.java Прегледај датотеку

@@ -0,0 +1,61 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public final class NaturalNumberTest {
7
+
8
+    @Test
9
+    public void testSmallPerfectNumberIsClassifiedCorrectly() {
10
+        assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification());
11
+    }
12
+
13
+    @Ignore
14
+    @Test
15
+    public void testMediumPerfectNumberIsClassifiedCorrectly() {
16
+        assertEquals(Classification.PERFECT, new NaturalNumber(28).getClassification());
17
+    }
18
+
19
+    @Ignore
20
+    @Test
21
+    public void testLargePerfectNumberIsClassifiedCorrectly() {
22
+        assertEquals(Classification.PERFECT, new NaturalNumber(33550336).getClassification());
23
+    }
24
+
25
+    @Ignore
26
+    @Test
27
+    public void testSmallAbundantNumberIsClassifiedCorrectly() {
28
+        assertEquals(Classification.ABUNDANT, new NaturalNumber(12).getClassification());
29
+    }
30
+
31
+    @Ignore
32
+    @Test
33
+    public void testMediumAbundantNumberIsClassifiedCorrectly() {
34
+        assertEquals(Classification.ABUNDANT, new NaturalNumber(24).getClassification());
35
+    }
36
+
37
+    @Ignore
38
+    @Test
39
+    public void testLargeAbundantNumberIsClassifiedCorrectly() {
40
+        assertEquals(Classification.ABUNDANT, new NaturalNumber(33550335).getClassification());
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void testSmallDeficientNumberIsClassifiedCorrectly() {
46
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(8).getClassification());
47
+    }
48
+
49
+    @Ignore
50
+    @Test
51
+    public void testMediumNumberIsClassifiedCorrectly() {
52
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(31).getClassification());
53
+    }
54
+
55
+    @Ignore
56
+    @Test
57
+    public void testLargeDeficientNumberIsClassifiedCorrectly() {
58
+        assertEquals(Classification.DEFICIENT, new NaturalNumber(33550337).getClassification());
59
+    }
60
+
61
+}

+ 1
- 0
exercises/settings.gradle Прегледај датотеку

@@ -34,6 +34,7 @@ include 'octal'
34 34
 include 'palindrome-products'
35 35
 include 'pangram'
36 36
 include 'pascals-triangle'
37
+include 'perfect-numbers'
37 38
 include 'phone-number'
38 39
 include 'pig-latin'
39 40
 include 'prime-factors'