Browse Source

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

update palindrome-products
Sam Warner 8 years ago
parent
commit
2f6cf2a166
No account linked to committer's email

+ 9
- 0
exercises/palindrome-products/.meta/src/reference/java/PalindromeCalculator.java View File

3
 public class PalindromeCalculator {
3
 public class PalindromeCalculator {
4
 
4
 
5
     public SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
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
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
10
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7
         long num;
11
         long num;
8
         List<List<Integer>> factors;
12
         List<List<Integer>> factors;
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
         return Collections.unmodifiableSortedMap(palindromeSortedList);
31
         return Collections.unmodifiableSortedMap(palindromeSortedList);
23
     }
32
     }
24
 
33
 

+ 1
- 0
exercises/palindrome-products/.meta/version View File

1
+1.1.0

+ 110
- 15
exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java View File

1
 import org.junit.Before;
1
 import org.junit.Before;
2
 import org.junit.Ignore;
2
 import org.junit.Ignore;
3
 import org.junit.Test;
3
 import org.junit.Test;
4
+import org.junit.Rule;
5
+import org.junit.rules.ExpectedException;
4
 
6
 
5
 import java.util.Arrays;
7
 import java.util.Arrays;
6
 import java.util.Collections;
8
 import java.util.Collections;
7
 import java.util.List;
9
 import java.util.List;
8
 import java.util.SortedMap;
10
 import java.util.SortedMap;
9
 import java.util.stream.Collectors;
11
 import java.util.stream.Collectors;
12
+import java.util.NoSuchElementException;
13
+
14
+import java.lang.IllegalArgumentException;
10
 
15
 
11
 import static org.junit.Assert.assertEquals;
16
 import static org.junit.Assert.assertEquals;
12
 import static org.junit.Assert.assertFalse;
17
 import static org.junit.Assert.assertFalse;
14
 
19
 
15
 public class PalindromeCalculatorTest {
20
 public class PalindromeCalculatorTest {
16
     private PalindromeCalculator palindromeCalculator;
21
     private PalindromeCalculator palindromeCalculator;
22
+    
23
+    @Rule
24
+    public ExpectedException expectedException = ExpectedException.none();
17
 
25
 
18
     @Before
26
     @Before
19
     public void setup() {
27
     public void setup() {
20
         palindromeCalculator = new PalindromeCalculator();
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
     @Test
46
     @Test
24
     public void largestPalindromeFromSingleDigitFactors() {
47
     public void largestPalindromeFromSingleDigitFactors() {
25
-        final List<List<Integer>> expected = Collections.unmodifiableList(
48
+        List<List<Integer>> expected = Collections.unmodifiableList(
26
                 Arrays.asList(
49
                 Arrays.asList(
27
                         Arrays.asList(1, 9),
50
                         Arrays.asList(1, 9),
28
                         Arrays.asList(3, 3)
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
58
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
36
     }
59
     }
38
     @Ignore("Remove to run test")
61
     @Ignore("Remove to run test")
39
     @Test
62
     @Test
40
     public void largestPalindromeFromDoubleDigitFactors() {
63
     public void largestPalindromeFromDoubleDigitFactors() {
41
-        final List<List<Integer>> expected = Collections.unmodifiableList(
64
+        List<List<Integer>> expected = Collections.unmodifiableList(
42
                 Arrays.asList(
65
                 Arrays.asList(
43
                         Arrays.asList(91, 99)
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
73
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
51
     }
74
     }
53
     @Ignore("Remove to run test")
76
     @Ignore("Remove to run test")
54
     @Test
77
     @Test
55
     public void smallestPalindromeFromDoubleDigitFactors() {
78
     public void smallestPalindromeFromDoubleDigitFactors() {
56
-        final List<List<Integer>> expected = Collections.unmodifiableList(
79
+        List<List<Integer>> expected = Collections.unmodifiableList(
57
                 Arrays.asList(
80
                 Arrays.asList(
58
                         Arrays.asList(11, 11)
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
88
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
66
     }
89
     }
68
     @Ignore("Remove to run test")
91
     @Ignore("Remove to run test")
69
     @Test
92
     @Test
70
     public void largestPalindromeFromTripleDigitFactors() {
93
     public void largestPalindromeFromTripleDigitFactors() {
71
-        final List<List<Integer>> expected = Collections.unmodifiableList(
94
+        List<List<Integer>> expected = Collections.unmodifiableList(
72
                 Arrays.asList(
95
                 Arrays.asList(
73
                         Arrays.asList(913, 993)
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
103
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
81
     }
104
     }
83
     @Ignore("Remove to run test")
106
     @Ignore("Remove to run test")
84
     @Test
107
     @Test
85
     public void smallestPalindromeFromTripleDigitFactors() {
108
     public void smallestPalindromeFromTripleDigitFactors() {
86
-        final List<List<Integer>> expected = Collections.unmodifiableList(
109
+        List<List<Integer>> expected = Collections.unmodifiableList(
87
                 Arrays.asList(
110
                 Arrays.asList(
88
                         Arrays.asList(101, 101)
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
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
     private void checkPalindromeWithFactorsMatchesExpected(List<List<Integer>> expectedPalindromeFactors,
193
     private void checkPalindromeWithFactorsMatchesExpected(List<List<Integer>> expectedPalindromeFactors,
99
                                                            long expectedValueOfPalindrome,
194
                                                            long expectedValueOfPalindrome,