瀏覽代碼

Added Pythagorean Triplet assignment (#187)

Dmitry Noranovich 9 年之前
父節點
當前提交
02d0ff33dd

+ 7
- 1
config.json 查看文件

@@ -50,7 +50,8 @@
50 50
     "minesweeper",
51 51
     "series",
52 52
     "robot-simulator",
53
-    "bracket-push"
53
+    "bracket-push",
54
+    "pythagorean-triplet"
54 55
   ],
55 56
   "exercises": [
56 57
     {
@@ -287,6 +288,11 @@
287 288
       "slug": "bracket-push",
288 289
       "difficulty": 1,
289 290
       "topics": []
291
+    },
292
+    {
293
+      "slug": "pythagorean-triplet",
294
+      "difficulty": 1,
295
+      "topics": []
290 296
     }
291 297
   ],
292 298
   "deprecated": [

+ 18
- 0
exercises/pythagorean-triplet/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
+}

+ 117
- 0
exercises/pythagorean-triplet/src/example/java/PythagoreanTriplet.java 查看文件

@@ -0,0 +1,117 @@
1
+
2
+import java.util.ArrayList;
3
+import java.util.List;
4
+import java.util.Objects;
5
+import java.util.stream.Collectors;
6
+
7
+public class PythagoreanTriplet {
8
+
9
+    private int a;
10
+    private int b;
11
+    private int c;
12
+
13
+    public PythagoreanTriplet(final int a, final int b, final int c) {
14
+        this.a = a;
15
+        this.b = b;
16
+        this.c = c;
17
+    }
18
+
19
+    public static TripletListBuilder makeTripletsList() {
20
+        return new TripletListBuilder();
21
+    }
22
+
23
+    public int calculateSum() {
24
+        return this.a + this.b + this.c;
25
+    }
26
+
27
+    public long calculateProduct() {
28
+        return this.a * this.b * this.c;
29
+    }
30
+
31
+    public boolean isPythagorean() {
32
+        return this.a * this.a + this.b * this.b == this.c * this.c;
33
+    }
34
+
35
+    @Override
36
+    public int hashCode() {
37
+        return Objects.hash(a, b, c);
38
+    }
39
+
40
+    @Override
41
+    public boolean equals(Object obj) {
42
+        if (this == obj) {
43
+            return true;
44
+        }
45
+        if (obj == null) {
46
+            return false;
47
+        }
48
+        if (getClass() != obj.getClass()) {
49
+            return false;
50
+        }
51
+        final PythagoreanTriplet other = (PythagoreanTriplet) obj;
52
+        if (this.a != other.a) {
53
+            return false;
54
+        }
55
+        if (this.b != other.b) {
56
+            return false;
57
+        }
58
+        if (this.c != other.c) {
59
+            return false;
60
+        }
61
+        return true;
62
+    }
63
+
64
+    public static class TripletListBuilder {
65
+
66
+        private static final int MAX_FACTOR_DEFAULT_VALUE = 0;
67
+        private static final int SUM_DEFAULT_VALUE = -1;
68
+
69
+        private int maxFactor = MAX_FACTOR_DEFAULT_VALUE;
70
+        private int minFactor = 1;
71
+        private int sum = SUM_DEFAULT_VALUE;
72
+
73
+        public TripletListBuilder() {
74
+        }
75
+
76
+        public TripletListBuilder withFactorsLessThanOrEqualTo(
77
+                final int maxFactor) {
78
+            this.maxFactor = maxFactor;
79
+            return this;
80
+        }
81
+
82
+        public TripletListBuilder withFactorsGreaterThanOrEqualTo(
83
+                final int minFactor) {
84
+            this.minFactor = minFactor;
85
+            return this;
86
+        }
87
+
88
+        public TripletListBuilder thatSumTo(final int sum) {
89
+            this.sum = sum;
90
+            return this;
91
+        }
92
+
93
+        public List<PythagoreanTriplet> build() {
94
+            List<PythagoreanTriplet> triplets = new ArrayList<>();
95
+            if (this.maxFactor == MAX_FACTOR_DEFAULT_VALUE) {
96
+                return triplets;
97
+            }
98
+            double sqrt;
99
+            int floor;
100
+            for (int n = minFactor; n < maxFactor; n++) {
101
+                for (int m = n + 1; m < maxFactor; m++) {
102
+                    sqrt = Math.sqrt(n * n + m * m);
103
+                    floor = (int) Math.floor(sqrt);
104
+                    if (sqrt == floor) {
105
+                        triplets.add(new PythagoreanTriplet(m, n, floor));
106
+                    }
107
+                }
108
+            }
109
+            if (sum != SUM_DEFAULT_VALUE) {
110
+                return triplets.stream()
111
+                        .filter(t -> t.calculateSum() == sum)
112
+                        .collect(Collectors.toList());
113
+            }
114
+            return triplets;
115
+        }
116
+    }
117
+}

+ 3
- 0
exercises/pythagorean-triplet/src/main/java/PythagoreanTriplet.java 查看文件

@@ -0,0 +1,3 @@
1
+
2
+public class PythagoreanTriplet {
3
+}

+ 95
- 0
exercises/pythagorean-triplet/src/test/java/PythagoreanTripletTest.java 查看文件

@@ -0,0 +1,95 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.HashMap;
4
+import java.util.List;
5
+import java.util.Map;
6
+import java.util.stream.Collectors;
7
+import static org.junit.Assert.assertEquals;
8
+import static org.junit.Assert.assertFalse;
9
+import static org.junit.Assert.assertTrue;
10
+import org.junit.Test;
11
+import org.junit.Ignore;
12
+
13
+public class PythagoreanTripletTest {
14
+
15
+    @Test
16
+    public void shouldCalculateSum() {
17
+        PythagoreanTriplet sut = new PythagoreanTriplet(3, 4, 5);
18
+        final int expected = 12;
19
+        final int actual = sut.calculateSum();
20
+        assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    @Ignore
25
+    public void shouldCalculateProduct() {
26
+        PythagoreanTriplet sut = new PythagoreanTriplet(3, 4, 5);
27
+        final long expected = 60l;
28
+        final long actual = sut.calculateProduct();
29
+        assertEquals(expected, actual);
30
+    }
31
+
32
+    @Test
33
+    @Ignore
34
+    public void testIsPythagoreanOK() {
35
+        PythagoreanTriplet sut = new PythagoreanTriplet(3, 4, 5);
36
+        assertTrue(sut.isPythagorean());
37
+    }
38
+
39
+    @Test
40
+    @Ignore
41
+    public void testIsPythagoreanFail() {
42
+        PythagoreanTriplet sut = new PythagoreanTriplet(5, 6, 7);
43
+        assertFalse(sut.isPythagorean());
44
+    }
45
+
46
+    @Test
47
+    @Ignore
48
+    public void shouldMakeTripletsUpToTen() {
49
+        final List<Long> actual
50
+                = PythagoreanTriplet
51
+                        .makeTripletsList()
52
+                        .withFactorsLessThanOrEqualTo(10)
53
+                        .build()
54
+                        .stream()
55
+                        .map(t -> t.calculateProduct())
56
+                        .sorted()
57
+                        .collect(Collectors.toList());
58
+        final List<Long> expected = Arrays.asList(60l, 480l);
59
+        assertEquals(expected, actual);
60
+    }
61
+
62
+    @Test
63
+    @Ignore
64
+    public void shouldMakeTripletsElevenToTwenty() {
65
+        final List<Long> actual
66
+                = PythagoreanTriplet
67
+                        .makeTripletsList()
68
+                        .withFactorsGreaterThanOrEqualTo(11)
69
+                        .withFactorsLessThanOrEqualTo(20)
70
+                        .build()
71
+                        .stream()
72
+                        .map(t -> t.calculateProduct())
73
+                        .sorted((p1, p2) -> Double.compare(p1, p2))
74
+                        .collect(Collectors.toList());
75
+        final List<Long> expected = Arrays.asList(3840l);
76
+        assertEquals(expected, actual);
77
+    }
78
+
79
+    @Test
80
+    @Ignore
81
+    public void shouldMakeTripletsAndFilterOnSum() {
82
+        final List<Long> actual
83
+                = PythagoreanTriplet
84
+                        .makeTripletsList()
85
+                        .withFactorsLessThanOrEqualTo(100)
86
+                        .thatSumTo(180)
87
+                        .build()
88
+                        .stream()
89
+                        .map(t -> t.calculateProduct())
90
+                        .sorted((p1, p2) -> Double.compare(p1, p2))
91
+                        .collect(Collectors.toList());
92
+        final List<Long> expected = Arrays.asList(118080l, 168480l, 202500l);
93
+        assertEquals(expected, actual);
94
+    }
95
+}

+ 2
- 0
exercises/settings.gradle 查看文件

@@ -5,6 +5,7 @@ include 'anagram'
5 5
 include 'atbash-cipher'
6 6
 include 'beer-song'
7 7
 include 'binary'
8
+include 'binary-search'
8 9
 include 'bob'
9 10
 include 'bracket-push'
10 11
 include 'crypto-square'
@@ -34,6 +35,7 @@ include 'robot-name'
34 35
 include 'robot-simulator'
35 36
 include 'roman-numerals'
36 37
 include 'prime-factors'
38
+include 'pythagorean-triplet'
37 39
 include 'scrabble-score'
38 40
 include 'series'
39 41
 include 'sieve'