|
1 |
| -import { Palindrome } from './palindrome-products'; |
| 1 | +import { Palindromes } from './palindrome-products'; |
2 | 2 |
|
3 |
| -describe('Palindrome', () => { |
4 |
| - test('largest palindrome from single digit factors', () => { |
5 |
| - const palindromes = Palindrome.generate({ maxFactor: 9 }); |
| 3 | +describe('Palindromes', () => { |
| 4 | + test('smallest palindrome from single digit factors', () => { |
| 5 | + const palindromes = Palindromes.generate({ maxFactor: 9, minFactor: 1 }); |
| 6 | + const smallest = palindromes.smallest; |
| 7 | + const expected = {value: 1, factors: [[1, 1]]}; |
| 8 | + |
| 9 | + expect(smallest.value).toEqual(expected.value); |
| 10 | + expect(sortFactors(smallest.factors)).toEqual(expected.factors); |
| 11 | + }); |
| 12 | + |
| 13 | + xtest('largest palindrome from single digit factors', () => { |
| 14 | + const palindromes = Palindromes.generate({ maxFactor: 9, minFactor: 1 }); |
6 | 15 | const largest = palindromes.largest;
|
| 16 | + const expected = {value: 9, factors: [[1, 9], [3, 3]]}; |
7 | 17 |
|
8 |
| - expect(largest.value).toEqual(9); |
9 |
| - const containsMatch = [[3, 3], [1, 9]].filter(el => numericalArraysMatch(el, largest.factors)).length > 0; |
10 |
| - expect(containsMatch).toBe(true); |
| 18 | + expect(largest.value).toEqual(expected.value); |
| 19 | + expect(sortFactors(largest.factors)).toEqual(expected.factors); |
| 20 | + }); |
| 21 | + |
| 22 | + xtest('smallest palindrome from double digit factors', () => { |
| 23 | + const palindromes = Palindromes.generate({ maxFactor: 99, minFactor: 10 }); |
| 24 | + const smallest = palindromes.smallest; |
| 25 | + const expected = {value: 121, factors: [[11, 11]]}; |
| 26 | + |
| 27 | + expect(smallest.value).toEqual(expected.value); |
| 28 | + expect(sortFactors(smallest.factors)).toEqual(expected.factors); |
11 | 29 | });
|
12 | 30 |
|
13 | 31 | xtest('largest palindrome from double digit factors', () => {
|
14 |
| - const palindromes = Palindrome.generate({ maxFactor: 99, minFactor: 10 }); |
| 32 | + const palindromes = Palindromes.generate({ maxFactor: 99, minFactor: 10 }); |
15 | 33 | const largest = palindromes.largest;
|
| 34 | + const expected = {value: 9009, factors: [[91, 99]]}; |
16 | 35 |
|
17 |
| - expect(largest.value).toEqual(9009); |
18 |
| - expect(largest.factors).toEqual([91, 99]); |
| 36 | + expect(largest.value).toEqual(expected.value); |
| 37 | + expect(sortFactors(largest.factors)).toEqual(expected.factors); |
19 | 38 | });
|
20 | 39 |
|
21 |
| - xtest('smallest palindrome from double digit factors', () => { |
22 |
| - const palindromes = Palindrome.generate({ maxFactor: 99, minFactor: 10 }); |
| 40 | + xtest('smallest palindrome from triple digit factors', () => { |
| 41 | + const palindromes = Palindromes.generate({ maxFactor: 999, minFactor: 100 }); |
23 | 42 | const smallest = palindromes.smallest;
|
| 43 | + const expected = {value: 10201, factors: [[101, 101]]}; |
24 | 44 |
|
25 |
| - expect(smallest.value).toEqual(121); |
26 |
| - expect(smallest.factors).toEqual([11, 11]); |
| 45 | + expect(smallest.value).toEqual(expected.value); |
| 46 | + expect(sortFactors(smallest.factors)).toEqual(expected.factors); |
27 | 47 | });
|
28 | 48 |
|
29 | 49 | xtest('largest palindrome from triple digit factors', () => {
|
30 |
| - const palindromes = Palindrome.generate({ maxFactor: 999, minFactor: 100 }); |
| 50 | + const palindromes = Palindromes.generate({ maxFactor: 999, minFactor: 100 }); |
31 | 51 | const largest = palindromes.largest;
|
| 52 | + const expected = {value: 906609, factors: [[913, 993]]}; |
32 | 53 |
|
33 |
| - expect(largest.value).toEqual(906609); |
34 |
| - expect(largest.factors).toEqual([913, 993]); |
| 54 | + expect(largest.value).toEqual(expected.value); |
| 55 | + expect(sortFactors(largest.factors)).toEqual(expected.factors); |
35 | 56 | });
|
36 | 57 |
|
37 |
| - xtest('smallest palindrome from triple digit factors', () => { |
38 |
| - const palindromes = Palindrome.generate({ maxFactor: 999, minFactor: 100 }); |
| 58 | + xtest('smallest palindrome from four digit factors', () => { |
| 59 | + const palindromes = Palindromes.generate({ maxFactor: 9999, minFactor: 1000 }); |
| 60 | + const smallest = palindromes.smallest; |
| 61 | + const expected = {value: 1002001, factors: [[1001, 1001]]}; |
| 62 | + |
| 63 | + expect(smallest.value).toEqual(expected.value); |
| 64 | + expect(sortFactors(smallest.factors)).toEqual(expected.factors); |
| 65 | + }); |
| 66 | + |
| 67 | + xtest('largest palindrome from four digit factors', () => { |
| 68 | + const palindromes = Palindromes.generate({ maxFactor: 9999, minFactor: 1000 }); |
| 69 | + const largest = palindromes.largest; |
| 70 | + const expected = {value: 99000099, factors: [[9901, 9999]]}; |
| 71 | + |
| 72 | + expect(largest.value).toEqual(expected.value); |
| 73 | + expect(sortFactors(largest.factors)).toEqual(expected.factors); |
| 74 | + }); |
| 75 | + |
| 76 | + xtest('empty result for smallest if no palindrome in range', () => { |
| 77 | + const palindromes = Palindromes.generate({ maxFactor: 1003, minFactor: 1002 }); |
39 | 78 | const smallest = palindromes.smallest;
|
40 | 79 |
|
41 |
| - expect(smallest.value).toEqual(10201); |
42 |
| - expect(smallest.factors).toEqual([101, 101]); |
| 80 | + expect(smallest.value).toBe(null); |
| 81 | + expect(smallest.factors).toEqual([]); |
| 82 | + }); |
| 83 | + |
| 84 | + xtest('empty result for largest if no palindrome in range', () => { |
| 85 | + const palindromes = Palindromes.generate({ maxFactor: 15, minFactor: 15 }); |
| 86 | + const largest = palindromes.largest; |
| 87 | + |
| 88 | + expect(largest.value).toBe(null); |
| 89 | + expect(largest.factors).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + xtest('error for smallest if min is more than max', () => { |
| 93 | + expect(() => { |
| 94 | + const palindromes = Palindromes.generate({ maxFactor: 1, minFactor: 10000 }); |
| 95 | + palindromes.smallest; |
| 96 | + }).toThrow(new Error('min must be <= max')); |
| 97 | + }); |
| 98 | + |
| 99 | + xtest('error for largest if min is more than max', () => { |
| 100 | + expect(() => { |
| 101 | + const palindromes = Palindromes.generate({ maxFactor: 1, minFactor: 2 }); |
| 102 | + palindromes.largest; |
| 103 | + }).toThrow(new Error('min must be <= max')); |
43 | 104 | });
|
44 | 105 | });
|
45 | 106 |
|
46 |
| -function numericalArraysMatch(a, b) { |
| 107 | +function factorsMatch(a, b) { |
47 | 108 | if (a.length !== b.length) {
|
48 | 109 | return false;
|
49 | 110 | }
|
50 |
| - const one = [...a].sort(numericalSort); |
51 |
| - const two = [...b].sort(numericalSort); |
52 |
| - let result = true; |
53 |
| - let index = 0; |
54 |
| - while (index < one.length) { |
55 |
| - result = result && one[index] === two[index]; |
56 |
| - index++; |
| 111 | + const one = a.map(f => f.sort()).sort(); |
| 112 | + const two = b.map(f => f.sort()).sort(); |
| 113 | + for (let i = 0; i < one.length; i += 1) { |
| 114 | + for (let j = 0; j < one[i].length; j += 1) { |
| 115 | + if (one[i][j] !== two[i][j]) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
57 | 119 | }
|
58 |
| - return result; |
| 120 | + return true; |
59 | 121 | }
|
60 | 122 |
|
61 |
| -function numericalSort(x, y) { |
62 |
| - if (x < y) { |
63 |
| - return -1; |
64 |
| - } |
65 |
| - if (x > y) { |
66 |
| - return 1; |
67 |
| - } |
68 |
| - return 0; |
| 123 | +function sortFactors(factors) { |
| 124 | + return factors.map(f => f.sort()).sort(); |
69 | 125 | }
|
0 commit comments