1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#!/usr/bin/env node
// ch-1.js
/*******************************************************************************
* https://theweeklychallenge.org/blog/perl-weekly-challenge-145/
*
* ## Task2 > Palindromic Tree
* ===========================
*
* You are given a string `$s`.
*
* Write a script to create a `Palindromic Tree` for the given string
*
* I found this [blog] explaining `Palindromic Tree` in detail.
*
* **Example 1:**
*
* ```
* Input: $s = 'redivider'
* Output: r redivider e edivide d divid i ivi v
* ```
*
* **Example 2:**
*
* ```
* Input: $s = 'deific'
* Output: d e i ifi f c
* ```
*
* **Example 3:**
*
* ```
* Input: $s = 'rotors'
* Output: r rotor o oto t s
* ```
*
* **Example 4:**
*
* ```
* Input: $s = 'challenge'
* Output: c h a l ll e n g
* ```
*
* **Example 5:**
*
* ```
* Input: $s = 'champion'
* Output: c h a m p i o n
* ```
*
* **Example 6**
*
* ```
* Input: $s = 'christmas'
* Output: c h r i s t m a
* ```
*
* [blog]: https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b
*
******************************************************************************/
'use strict';
const fs = require('fs');
const path = require('path');
/*******************************************************************************
* PWC Solution ****************************************************************
******************************************************************************/
function getPalindromes(str = '') {
const isPalindrome = (s = '') => s === s.split('').reverse().join('');
const palindromes = [];
[...[...str].keys()].forEach((idx) => {
const substr = str.slice(0, idx + 1);
if (isPalindrome(substr)) palindromes.push(substr);
});
return palindromes;
}
// The blog mentioned in the challenge description describes a pretty
// interesting data structure to tackle this challenge. My approach here is not
// nearly as nice.
function eertree(str = '') {
let palindromes = [];
[...[...str].keys()].forEach((idx) => {
const substr = str.slice(idx);
const subPalindromes = getPalindromes(substr);
palindromes = Array.from(new Set([...palindromes, ...subPalindromes]));
});
return palindromes.join(' ');
}
/*******************************************************************************
* Utilities *******************************************************************
******************************************************************************/
function parseTestCase(filePath = '') {
try {
const data = fs.readFileSync(filePath, 'utf8');
const lines = data.split('\n');
const [input, output] = lines.filter(
(line) => line.trim().charAt(0) !== '#',
);
return [input.trim(), output.trim()];
} catch (error) {
console.log(`Problems parsing ${filePath}: ${error}`);
return [];
}
}
function printParams(filePath = '', input = '', output = '') {
console.log(`${filePath}:\nInput: $s = ${input}\nOutput: ${output}`);
return true;
}
function testSolution(solution = () => {}, input = '', test = '') {
const result = solution(input);
if (test === result) {
return console.log('\x1b[32m%s\x1b[0m', 'Passed \u2690\n');
}
return console.log('\x1b[31m%s\x1b[0m', 'Failed \u2715\n');
}
function isFile(filePath) {
return fs.lstatSync(filePath).isFile();
}
function isDirectory(filePath) {
return fs.lstatSync(filePath).isDirectory();
}
/*******************************************************************************
* Main ************************************************************************
******************************************************************************/
(function main() {
const testPath = process.argv[2] || '../test_cases/ch-2';
try {
if (isFile(testPath)) {
const [input, test] = parseTestCase(testPath);
return (
input &&
test &&
printParams(testPath, input, test) &&
testSolution(eertree, input, test)
);
}
if (isDirectory(testPath)) {
return fs.readdirSync(testPath).forEach((fileName) => {
const filePath = path.join(testPath, fileName);
const [input, test] = parseTestCase(filePath);
return (
input &&
test &&
printParams(filePath, input, test) &&
testSolution(eertree, input, test)
);
});
}
return 1;
} catch (error) {
return console.log('Something went wrong:', error);
}
})();
|