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

Merge pull request #1283 from jssander/fix-palindrome-products

update palindrome-products
Sam Warner 8 лет назад
Родитель
Сommit
2f6cf2a166
Аккаунт пользователя с таким Email не найден

+ 9
- 0
exercises/palindrome-products/.meta/src/reference/java/PalindromeCalculator.java Просмотреть файл

@@ -3,6 +3,10 @@ import java.util.*;
3 3
 public class PalindromeCalculator {
4 4
 
5 5
     public SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
6
+    	if(minFactor > maxFactor) {
7
+    		throw new IllegalArgumentException("invalid input: min is " + minFactor + " and max is " + maxFactor);
8
+    	}
9
+    	
6 10
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7 11
         long num;
8 12
         List<List<Integer>> factors;
@@ -19,6 +23,11 @@ public class PalindromeCalculator {
19 23
                 }
20 24
             }
21 25
         }
26
+        
27
+        if(palindromeSortedList.size() == 0) {
28
+    		throw new NoSuchElementException("no palindrome with factors in the range " + minFactor + " to " + maxFactor);
29
+    	}
30
+        
22 31
         return Collections.unmodifiableSortedMap(palindromeSortedList);
23 32
     }
24 33
 

+ 1
- 0
exercises/palindrome-products/.meta/version Просмотреть файл

@@ -0,0 +1 @@
1
+1.1.0

+ 110
- 15
exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java Просмотреть файл

@@ -1,12 +1,17 @@
1 1
 import org.junit.Before;
2 2
 import org.junit.Ignore;
3 3
 import org.junit.Test;
4
+import org.junit.Rule;
5
+import org.junit.rules.ExpectedException;
4 6
 
5 7
 import java.util.Arrays;
6 8
 import java.util.Collections;
7 9
 import java.util.List;
8 10
 import java.util.SortedMap;
9 11
 import java.util.stream.Collectors;
12
+import java.util.NoSuchElementException;
13
+
14
+import java.lang.IllegalArgumentException;
10 15
 
11 16
 import static org.junit.Assert.assertEquals;
12 17
 import static org.junit.Assert.assertFalse;
@@ -14,23 +19,41 @@ import static org.junit.Assert.assertNotNull;
14 19
 
15 20
 public class PalindromeCalculatorTest {
16 21
     private PalindromeCalculator palindromeCalculator;
22
+    
23
+    @Rule
24
+    public ExpectedException expectedException = ExpectedException.none();
17 25
 
18 26
     @Before
19 27
     public void setup() {
20 28
         palindromeCalculator = new PalindromeCalculator();
21 29
     }
30
+    
31
+    @Test
32
+    public void smallestPalindromeFromSingleDigitFactors() {
33
+        List<List<Integer>> expected = Collections.unmodifiableList(
34
+                Arrays.asList(
35
+                        Arrays.asList(1, 1)
36
+                )
37
+        );
38
+        long expectedValue = 1l;
39
+
40
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
22 41
 
42
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
43
+    }
44
+
45
+    @Ignore("Remove to run test")
23 46
     @Test
24 47
     public void largestPalindromeFromSingleDigitFactors() {
25
-        final List<List<Integer>> expected = Collections.unmodifiableList(
48
+        List<List<Integer>> expected = Collections.unmodifiableList(
26 49
                 Arrays.asList(
27 50
                         Arrays.asList(1, 9),
28 51
                         Arrays.asList(3, 3)
29 52
                 )
30 53
         );
31
-        final long expectedValue = 9l;
54
+        long expectedValue = 9l;
32 55
 
33
-        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
56
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
34 57
 
35 58
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
36 59
     }
@@ -38,14 +61,14 @@ public class PalindromeCalculatorTest {
38 61
     @Ignore("Remove to run test")
39 62
     @Test
40 63
     public void largestPalindromeFromDoubleDigitFactors() {
41
-        final List<List<Integer>> expected = Collections.unmodifiableList(
64
+        List<List<Integer>> expected = Collections.unmodifiableList(
42 65
                 Arrays.asList(
43 66
                         Arrays.asList(91, 99)
44 67
                 )
45 68
         );
46
-        final long expectedValue = 9009l;
69
+        long expectedValue = 9009l;
47 70
 
48
-        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
71
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
49 72
 
50 73
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
51 74
     }
@@ -53,14 +76,14 @@ public class PalindromeCalculatorTest {
53 76
     @Ignore("Remove to run test")
54 77
     @Test
55 78
     public void smallestPalindromeFromDoubleDigitFactors() {
56
-        final List<List<Integer>> expected = Collections.unmodifiableList(
79
+        List<List<Integer>> expected = Collections.unmodifiableList(
57 80
                 Arrays.asList(
58 81
                         Arrays.asList(11, 11)
59 82
                 )
60 83
         );
61
-        final long expectedValue = 121l;
84
+        long expectedValue = 121l;
62 85
 
63
-        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
86
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
64 87
 
65 88
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
66 89
     }
@@ -68,14 +91,14 @@ public class PalindromeCalculatorTest {
68 91
     @Ignore("Remove to run test")
69 92
     @Test
70 93
     public void largestPalindromeFromTripleDigitFactors() {
71
-        final List<List<Integer>> expected = Collections.unmodifiableList(
94
+        List<List<Integer>> expected = Collections.unmodifiableList(
72 95
                 Arrays.asList(
73 96
                         Arrays.asList(913, 993)
74 97
                 )
75 98
         );
76
-        final long expectedValue = 906609l;
99
+        long expectedValue = 906609l;
77 100
 
78
-        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
101
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
79 102
 
80 103
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
81 104
     }
@@ -83,17 +106,89 @@ public class PalindromeCalculatorTest {
83 106
     @Ignore("Remove to run test")
84 107
     @Test
85 108
     public void smallestPalindromeFromTripleDigitFactors() {
86
-        final List<List<Integer>> expected = Collections.unmodifiableList(
109
+        List<List<Integer>> expected = Collections.unmodifiableList(
87 110
                 Arrays.asList(
88 111
                         Arrays.asList(101, 101)
89 112
                 )
90 113
         );
91
-        final long expectedValue = 10201l;
114
+        long expectedValue = 10201l;
115
+
116
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
117
+
118
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
119
+    }
120
+    
121
+    @Ignore("Remove to run test")
122
+    @Test
123
+    public void smallestPalindromeFromQuadDigitFactors() {
124
+        List<List<Integer>> expected = Collections.unmodifiableList(
125
+                Arrays.asList(
126
+                        Arrays.asList(1001, 1001)
127
+                )
128
+        );
129
+        long expectedValue = 1002001l;
92 130
 
93
-        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
131
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1000, 9999);
94 132
 
95 133
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
96 134
     }
135
+    
136
+    @Ignore("Remove to run test")
137
+    @Test
138
+    public void largestPalindromeFromQuadDigitFactors() {
139
+        List<List<Integer>> expected = Collections.unmodifiableList(
140
+                Arrays.asList(
141
+                        Arrays.asList(9901, 9999)
142
+                )
143
+        );
144
+        long expectedValue = 99000099l;
145
+
146
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1000, 9999);
147
+
148
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
149
+    }
150
+    
151
+    @Ignore("Remove to run test")
152
+    @Test
153
+    public void emtpyResultSmallestNoPalindromeInRange() {
154
+        
155
+        expectedException.expect(NoSuchElementException.class);
156
+        expectedException.expectMessage("no palindrome with factors in the range 1002 to 1003");
157
+
158
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1002, 1003);
159
+    }
160
+    
161
+    @Ignore("Remove to run test")
162
+    @Test
163
+    public void emtpyResultLargestNoPalindromeInRange() {
164
+        
165
+        expectedException.expect(NoSuchElementException.class);
166
+        expectedException.expectMessage("no palindrome with factors in the range 15 to 15");
167
+
168
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(15, 15);
169
+    }
170
+    
171
+    @Ignore("Remove to run test")
172
+    @Test
173
+    public void errorSmallestMinIsMoreThanMax() {
174
+        
175
+        expectedException.expect(IllegalArgumentException.class);
176
+        expectedException.expectMessage("invalid input: min is 10000 and max is 1");
177
+
178
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10000, 1);
179
+    }
180
+    
181
+    @Ignore("Remove to run test")
182
+    @Test
183
+    public void errorLargestMinIsMoreThanMax() {
184
+        
185
+        expectedException.expect(IllegalArgumentException.class);
186
+        expectedException.expectMessage("invalid input: min is 2 and max is 1");
187
+
188
+        SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(2, 1);
189
+    }
190
+    
191
+    
97 192
 
98 193
     private void checkPalindromeWithFactorsMatchesExpected(List<List<Integer>> expectedPalindromeFactors,
99 194
                                                            long expectedValueOfPalindrome,