Przeglądaj źródła

update palindrome-products

Jeffrey Sander 8 lat temu
rodzic
commit
e2c4a808b5

+ 5
- 0
exercises/palindrome-products/.meta/src/reference/java/PalindromeCalculator.java Wyświetl plik

@@ -3,6 +3,8 @@ 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) throw new IllegalArgumentException("invalid input: min is " + minFactor + " and max is " + maxFactor);
7
+    	
6 8
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7 9
         long num;
8 10
         List<List<Integer>> factors;
@@ -19,6 +21,9 @@ public class PalindromeCalculator {
19 21
                 }
20 22
             }
21 23
         }
24
+        
25
+        if(palindromeSortedList.size() == 0) throw new NoSuchElementException("no palindrome with factors in the range " + minFactor + " to " + maxFactor);
26
+        
22 27
         return Collections.unmodifiableSortedMap(palindromeSortedList);
23 28
     }
24 29
 

+ 1
- 0
exercises/palindrome-products/.meta/version Wyświetl plik

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

+ 95
- 0
exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java Wyświetl plik

@@ -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,12 +19,30 @@ 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
+        final List<List<Integer>> expected = Collections.unmodifiableList(
34
+                Arrays.asList(
35
+                        Arrays.asList(1, 1)
36
+                )
37
+        );
38
+        final long expectedValue = 1l;
39
+
40
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
41
+
42
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
43
+    }
22 44
 
45
+    @Ignore("Remove to run test")
23 46
     @Test
24 47
     public void largestPalindromeFromSingleDigitFactors() {
25 48
         final List<List<Integer>> expected = Collections.unmodifiableList(
@@ -94,6 +117,78 @@ public class PalindromeCalculatorTest {
94 117
 
95 118
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
96 119
     }
120
+    
121
+    @Ignore("Remove to run test")
122
+    @Test
123
+    public void smallestPalindromeFromQuadDigitFactors() {
124
+        final List<List<Integer>> expected = Collections.unmodifiableList(
125
+                Arrays.asList(
126
+                        Arrays.asList(1001, 1001)
127
+                )
128
+        );
129
+        final long expectedValue = 1002001l;
130
+
131
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1000, 9999);
132
+
133
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
134
+    }
135
+    
136
+    @Ignore("Remove to run test")
137
+    @Test
138
+    public void largestPalindromeFromQuadDigitFactors() {
139
+        final List<List<Integer>> expected = Collections.unmodifiableList(
140
+                Arrays.asList(
141
+                        Arrays.asList(9901, 9999)
142
+                )
143
+        );
144
+        final long expectedValue = 99000099l;
145
+
146
+        final 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
+        final 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
+        final 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
+        final 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
+        final 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,