浏览代码

Added palindrome-products exercise to the Java track (#205)

* Added palindrome-products exercise to the Java track

* Added @Ignore annotations to tests
Dmitry Noranovich 9 年前
父节点
当前提交
d52d83a339

+ 7
- 1
config.json 查看文件

@@ -54,7 +54,8 @@
54 54
     "pythagorean-triplet",
55 55
     "binary-search-tree",
56 56
     "binary-search",
57
-    "all-your-base"
57
+    "all-your-base",
58
+    "palindrome-products"
58 59
   ],
59 60
   "exercises": [
60 61
     {
@@ -311,6 +312,11 @@
311 312
       "slug": "all-your-base",
312 313
       "difficulty": 1,
313 314
       "topics": []
315
+    },
316
+    {
317
+      "slug": "palindrome-products",
318
+      "difficulty": 1,
319
+      "topics": []
314 320
     }
315 321
   ],
316 322
   "deprecated": [

+ 18
- 0
exercises/palindrome-products/build.gradle 查看文件

@@ -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
+}

+ 77
- 0
exercises/palindrome-products/src/example/java/Palindromes.java 查看文件

@@ -0,0 +1,77 @@
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;
8
+
9
+public class Palindromes {
10
+
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);
45
+                    }
46
+                }
47
+            }
48
+            return Collections.unmodifiableSortedMap(palindromes);
49
+        }
50
+
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;
64
+        }
65
+
66
+        private boolean isPalindrome(long number) {
67
+            return number == reverseNumber(number);
68
+        }
69
+    }
70
+
71
+    private Palindromes() {
72
+    }
73
+
74
+    public static PalindromeSortedSetBuilder getPalindromeSortedListBuilder() {
75
+        return new PalindromeSortedSetBuilder();
76
+    }
77
+}

+ 0
- 0
exercises/palindrome-products/src/main/java/.keep 查看文件


+ 162
- 0
exercises/palindrome-products/src/test/java/PalindromesTest.java 查看文件

@@ -0,0 +1,162 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import java.util.List;
5
+import java.util.SortedMap;
6
+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;
12
+
13
+public class PalindromesTest {
14
+
15
+    @Test
16
+    public void largestPalindromeFromSingleDigitFactors() {
17
+        final List<List<Integer>> expected = Collections.unmodifiableList(
18
+                Arrays.asList(
19
+                        Arrays.asList(1, 9),
20
+                        Arrays.asList(3, 3)
21
+                )
22
+        );
23
+        final long expectedValue = 9l;
24
+
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);
34
+
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);
41
+    }
42
+
43
+    @Test
44
+    @Ignore
45
+    public void largestPalindromeFromDoubleDigitFactors() {
46
+        final List<List<Integer>> expected = Collections.unmodifiableList(
47
+                Arrays.asList(
48
+                        Arrays.asList(91, 99)
49
+                )
50
+        );
51
+        final long expectedValue = 9009l;
52
+
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());
61
+
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);
71
+    }
72
+
73
+    @Test
74
+    @Ignore
75
+    public void smallestPalindromeFromDoubleDigitFactors() {
76
+        final List<List<Integer>> expected = Collections.unmodifiableList(
77
+                Arrays.asList(
78
+                        Arrays.asList(11, 11)
79
+                )
80
+        );
81
+        final long expectedValue = 121l;
82
+
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());
91
+
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);
101
+    }
102
+
103
+    @Test
104
+    @Ignore
105
+    public void largestPalindromeFromTripleDigitFactors() {
106
+        final List<List<Integer>> expected = Collections.unmodifiableList(
107
+                Arrays.asList(
108
+                        Arrays.asList(913, 993)
109
+                )
110
+        );
111
+        final long expectedValue = 906609l;
112
+
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());
121
+
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);
131
+    }
132
+
133
+    @Test
134
+    @Ignore
135
+    public void smallestPalindromeFromTripleDigitFactors() {
136
+        final List<List<Integer>> expected = Collections.unmodifiableList(
137
+                Arrays.asList(
138
+                        Arrays.asList(101, 101)
139
+                )
140
+        );
141
+        final long expectedValue = 10201l;
142
+
143
+        final SortedMap<Long, List<List<Integer>>> palindromes
144
+                = Palindromes.getPalindromeSortedListBuilder()
145
+                        .withFactorsLessThanOrEqualTo(999)
146
+                        .withFactorsGreaterThanOrEqualTo(100)
147
+                        .build();
148
+
149
+        assertNotNull(palindromes);
150
+        assertFalse(palindromes.isEmpty());
151
+
152
+        long actualValue = palindromes.firstKey();
153
+        assertEquals(expectedValue, actualValue);
154
+
155
+        List<List<Integer>> actual = palindromes
156
+                .get(palindromes.firstKey())
157
+                .stream()
158
+                .sorted((a, b) -> Integer.compare(a.get(0), b.get(0)))
159
+                .collect(Collectors.toList());
160
+        assertEquals(expected, actual);
161
+    }
162
+}

+ 1
- 0
exercises/settings.gradle 查看文件

@@ -25,6 +25,7 @@ include 'meetup'
25 25
 include 'nth-prime'
26 26
 include 'nucleotide-count'
27 27
 include 'octal'
28
+include 'palindrome-products'
28 29
 include 'pangram'
29 30
 include 'pascals-triangle'
30 31
 include 'phone-number'