Skip to content

Commit 02d0ff3

Browse files
javaeeeeejtigger
authored andcommitted
Added Pythagorean Triplet assignment (#187)
1 parent 6c18cc5 commit 02d0ff3

File tree

6 files changed

+242
-1
lines changed

6 files changed

+242
-1
lines changed

config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"minesweeper",
5151
"series",
5252
"robot-simulator",
53-
"bracket-push"
53+
"bracket-push",
54+
"pythagorean-triplet"
5455
],
5556
"exercises": [
5657
{
@@ -287,6 +288,11 @@
287288
"slug": "bracket-push",
288289
"difficulty": 1,
289290
"topics": []
291+
},
292+
{
293+
"slug": "pythagorean-triplet",
294+
"difficulty": 1,
295+
"topics": []
290296
}
291297
],
292298
"deprecated": [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
public class PythagoreanTriplet {
3+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

exercises/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ include 'anagram'
55
include 'atbash-cipher'
66
include 'beer-song'
77
include 'binary'
8+
include 'binary-search'
89
include 'bob'
910
include 'bracket-push'
1011
include 'crypto-square'
@@ -34,6 +35,7 @@ include 'robot-name'
3435
include 'robot-simulator'
3536
include 'roman-numerals'
3637
include 'prime-factors'
38+
include 'pythagorean-triplet'
3739
include 'scrabble-score'
3840
include 'series'
3941
include 'sieve'

0 commit comments

Comments
 (0)