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

Simplify construction logic for palindrome products (#305)

* Simplify construction logic for palindrome products

Fixes issue #283

* Refactor palindrome test comparator into a named function
FridaTveit 9 лет назад
Родитель
Сommit
95ede81388

+ 28
- 62
exercises/palindrome-products/src/example/java/Palindromes.java Просмотреть файл

@@ -1,77 +1,43 @@
1
-
2
-import java.util.ArrayList;
3
-import java.util.Arrays;
4
-import java.util.Collections;
5
-import java.util.List;
6
-import java.util.SortedMap;
7
-import java.util.TreeMap;
1
+import java.util.*;
8 2
 
9 3
 public class Palindromes {
10 4
 
11
-    public static class PalindromeSortedSetBuilder {
12
-
13
-        private int minFactor = 1;
14
-        private int maxFactor = 1;
15
-
16
-        public PalindromeSortedSetBuilder() {
17
-        }
18
-
19
-        public PalindromeSortedSetBuilder withFactorsLessThanOrEqualTo(
20
-                final int maxFactor) {
21
-            this.maxFactor = maxFactor;
22
-            return this;
23
-        }
24
-
25
-        public PalindromeSortedSetBuilder withFactorsGreaterThanOrEqualTo(
26
-                final int minFactor) {
27
-            this.minFactor = minFactor;
28
-            return this;
29
-        }
30
-
31
-        public SortedMap<Long, List<List<Integer>>> build() {
32
-            SortedMap<Long, List<List<Integer>>> palindromes = new TreeMap<>();
33
-            long num;
34
-            List<List<Integer>> factors;
35
-            for (int i = this.maxFactor; i >= this.minFactor; i--) {
36
-                for (int j = this.maxFactor; j >= i; j--) {
37
-                    num = i * j;
38
-                    if (isPalindrome(num)) {
39
-                        factors = palindromes.get(num);
40
-                        if (factors == null) {
41
-                            factors = new ArrayList<>();
42
-                        }
43
-                        factors.add(Arrays.asList(i, j));
44
-                        palindromes.put(num, factors);
5
+    public static SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
6
+        SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7
+        long num;
8
+        List<List<Integer>> factors;
9
+        for (int i = maxFactor; i >= minFactor; i--) {
10
+            for (int j = maxFactor; j >= i; j--) {
11
+                num = i * j;
12
+                if (isPalindrome(num)) {
13
+                    factors = palindromeSortedList.get(num);
14
+                    if (factors == null) {
15
+                        factors = new ArrayList<>();
45 16
                     }
17
+                    factors.add(Arrays.asList(i, j));
18
+                    palindromeSortedList.put(num, factors);
46 19
                 }
47 20
             }
48
-            return Collections.unmodifiableSortedMap(palindromes);
49 21
         }
22
+        return Collections.unmodifiableSortedMap(palindromeSortedList);
23
+    }
50 24
 
51
-        // http://stackoverflow.com/questions/23984654/how-to-print-all-palindromes-upto-1000-without-using-any-string-stringbuilder
52
-        private long reverseNumber(long number) {
53
-            if (number < 10l) {
54
-                return number;
55
-            }
56
-            long result = 0;
57
-            long tmp = number;
58
-            while (tmp > 0) {
59
-                result = result * 10 + (tmp % 10);
60
-                tmp /= 10;
61
-
62
-            }
63
-            return result;
25
+    // http://stackoverflow.com/questions/23984654/how-to-print-all-palindromes-upto-1000-without-using-any-string-stringbuilder
26
+    private static long reverseNumber(long number) {
27
+        if (number < 10l) {
28
+            return number;
64 29
         }
30
+        long result = 0;
31
+        long tmp = number;
32
+        while (tmp > 0) {
33
+            result = result * 10 + (tmp % 10);
34
+            tmp /= 10;
65 35
 
66
-        private boolean isPalindrome(long number) {
67
-            return number == reverseNumber(number);
68 36
         }
37
+        return result;
69 38
     }
70 39
 
71
-    private Palindromes() {
72
-    }
73
-
74
-    public static PalindromeSortedSetBuilder getPalindromeSortedListBuilder() {
75
-        return new PalindromeSortedSetBuilder();
40
+    private static boolean isPalindrome(long number) {
41
+        return number == reverseNumber(number);
76 42
     }
77 43
 }

+ 26
- 83
exercises/palindrome-products/src/test/java/PalindromesTest.java Просмотреть файл

@@ -1,14 +1,13 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
1 3
 
2 4
 import java.util.Arrays;
3 5
 import java.util.Collections;
4 6
 import java.util.List;
5 7
 import java.util.SortedMap;
6 8
 import java.util.stream.Collectors;
7
-import static junit.framework.TestCase.assertEquals;
8
-import static junit.framework.TestCase.assertFalse;
9
-import static junit.framework.TestCase.assertNotNull;
10
-import org.junit.Ignore;
11
-import org.junit.Test;
9
+
10
+import static junit.framework.TestCase.*;
12 11
 
13 12
 public class PalindromesTest {
14 13
 
@@ -22,22 +21,9 @@ public class PalindromesTest {
22 21
         );
23 22
         final long expectedValue = 9l;
24 23
 
25
-        final SortedMap<Long, List<List<Integer>>> palindromes
26
-                = Palindromes.getPalindromeSortedListBuilder()
27
-                        .withFactorsLessThanOrEqualTo(9).build();
28
-
29
-        assertNotNull(palindromes);
30
-        assertFalse(palindromes.isEmpty());
31
-
32
-        long actualValue = palindromes.lastKey();
33
-        assertEquals(expectedValue, actualValue);
24
+        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(1, 9);
34 25
 
35
-        List<List<Integer>> actual = palindromes
36
-                .get(palindromes.lastKey())
37
-                .stream()
38
-                .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
39
-                .collect(Collectors.toList());
40
-        assertEquals(expected, actual);
26
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
41 27
     }
42 28
 
43 29
     @Test
@@ -50,24 +36,9 @@ public class PalindromesTest {
50 36
         );
51 37
         final long expectedValue = 9009l;
52 38
 
53
-        final SortedMap<Long, List<List<Integer>>> palindromes
54
-                = Palindromes.getPalindromeSortedListBuilder()
55
-                        .withFactorsLessThanOrEqualTo(99)
56
-                        .withFactorsGreaterThanOrEqualTo(10)
57
-                        .build();
58
-
59
-        assertNotNull(palindromes);
60
-        assertFalse(palindromes.isEmpty());
39
+        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
61 40
 
62
-        long actualValue = palindromes.lastKey();
63
-        assertEquals(expectedValue, actualValue);
64
-
65
-        List<List<Integer>> actual = palindromes
66
-                .get(palindromes.lastKey())
67
-                .stream()
68
-                .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
69
-                .collect(Collectors.toList());
70
-        assertEquals(expected, actual);
41
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
71 42
     }
72 43
 
73 44
     @Test
@@ -80,24 +51,9 @@ public class PalindromesTest {
80 51
         );
81 52
         final long expectedValue = 121l;
82 53
 
83
-        final SortedMap<Long, List<List<Integer>>> palindromes
84
-                = Palindromes.getPalindromeSortedListBuilder()
85
-                        .withFactorsLessThanOrEqualTo(99)
86
-                        .withFactorsGreaterThanOrEqualTo(10)
87
-                        .build();
88
-
89
-        assertNotNull(palindromes);
90
-        assertFalse(palindromes.isEmpty());
54
+        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
91 55
 
92
-        long actualValue = palindromes.firstKey();
93
-        assertEquals(expectedValue, actualValue);
94
-
95
-        List<List<Integer>> actual = palindromes
96
-                .get(palindromes.firstKey())
97
-                .stream()
98
-                .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
99
-                .collect(Collectors.toList());
100
-        assertEquals(expected, actual);
56
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
101 57
     }
102 58
 
103 59
     @Test
@@ -110,24 +66,9 @@ public class PalindromesTest {
110 66
         );
111 67
         final long expectedValue = 906609l;
112 68
 
113
-        final SortedMap<Long, List<List<Integer>>> palindromes
114
-                = Palindromes.getPalindromeSortedListBuilder()
115
-                        .withFactorsLessThanOrEqualTo(999)
116
-                        .withFactorsGreaterThanOrEqualTo(100)
117
-                        .build();
118
-
119
-        assertNotNull(palindromes);
120
-        assertFalse(palindromes.isEmpty());
69
+        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
121 70
 
122
-        long actualValue = palindromes.lastKey();
123
-        assertEquals(expectedValue, actualValue);
124
-
125
-        List<List<Integer>> actual = palindromes
126
-                .get(palindromes.lastKey())
127
-                .stream()
128
-                .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
129
-                .collect(Collectors.toList());
130
-        assertEquals(expected, actual);
71
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
131 72
     }
132 73
 
133 74
     @Test
@@ -140,23 +81,25 @@ public class PalindromesTest {
140 81
         );
141 82
         final long expectedValue = 10201l;
142 83
 
143
-        final SortedMap<Long, List<List<Integer>>> palindromes
144
-                = Palindromes.getPalindromeSortedListBuilder()
145
-                        .withFactorsLessThanOrEqualTo(999)
146
-                        .withFactorsGreaterThanOrEqualTo(100)
147
-                        .build();
84
+        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
85
+
86
+        checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
87
+    }
148 88
 
149
-        assertNotNull(palindromes);
150
-        assertFalse(palindromes.isEmpty());
89
+    private void checkPalindromeWithFactorsMatchesExpected(List<List<Integer>> expectedPalindromeFactors,
90
+                                                           long expectedValueOfPalindrome,
91
+                                                           SortedMap<Long, List<List<Integer>>> actualPalindromes,
92
+                                                           long actualValueOfPalindrome) {
93
+        assertNotNull(actualPalindromes);
94
+        assertFalse(actualPalindromes.isEmpty());
151 95
 
152
-        long actualValue = palindromes.firstKey();
153
-        assertEquals(expectedValue, actualValue);
96
+        assertEquals(expectedValueOfPalindrome, actualValueOfPalindrome);
154 97
 
155
-        List<List<Integer>> actual = palindromes
156
-                .get(palindromes.firstKey())
98
+        List<List<Integer>> actualPalindromeFactors = actualPalindromes
99
+                .get(actualValueOfPalindrome)
157 100
                 .stream()
158 101
                 .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
159 102
                 .collect(Collectors.toList());
160
-        assertEquals(expected, actual);
103
+        assertEquals(expectedPalindromeFactors, actualPalindromeFactors);
161 104
     }
162 105
 }