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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
#!/usr/bin/env node
// ch-2.js
/*******************************************************************************
* https://theweeklychallenge.org/blog/perl-weekly-challenge-146/
*
* ## Task 2 > Curious Fraction Tree
* =================================
*
* Consider the following `Curious Fraction Tree`:
*
* ```
* __________1/1__________
* / \
* ___1/2___ ___2/1___
* / \ / \
* 1/3 3/2 2/3 3/1
* / \ / \ / \ / \
* 1/4 4/3 3/5 5/2 2/5 5/3 3/4 4/1
* ```
*
* You are given a fraction, member of the tree created similar to the above
* sample.
*
* Write a script to find out the parent and grandparent of the given member.
*
* **Example 1:**
*
* ```
* Input: $member = '3/5';
* Output: parent = '3/2' and grandparent = '1/2'
* ```
*
* **Example 2:**
*
* ```
* Input: $member = '4/3';
* Output: parent = '1/3' and grandparent = '1/2'
* ```
*
******************************************************************************/
'use strict';
/*******************************************************************************
* Dependencies ****************************************************************
******************************************************************************/
const readline = require('readline');
/*******************************************************************************
* PWC Solution ****************************************************************
******************************************************************************/
// Input guard
function isFractionString(string) {
const re = /^\d+\/\d+$/;
return typeof string === 'string' && re.test(string.trim());
}
// Find immediate parent
function getCuriousParent(member = '1/1') {
if (!isFractionString(member)) {
const badArg = '`getCuriousParent` expects a string (i.e.: "3/4")';
throw new Error(badArg);
}
const [num, den] = member.split('/').map((elem) => parseInt(elem, 10));
const value = num / den;
if (value > 1) return `${num - den}/${den}`;
if (value < 1) return `${num}/${den - num}`;
return null;
}
// Returns whole chain from member to root
function getCuriousTree(member = '1/1') {
if (!isFractionString(member)) {
throw new Error('`getCuriousTree` expects a string (i.e.: "3/2")');
}
const parents = [];
let child = member;
while (child !== null) {
parents.push(child);
child = getCuriousParent(child);
}
return parents;
}
// Not really necessarily to abstract this step, but it makes it easier to do
// other things with the whole chain, like figure out if it is rooted at 1/1 or
// not
function getCuriousGenerations(tree = [], generations = 2) {
return tree.slice(1, generations + 1);
}
/*******************************************************************************
* Utilities *******************************************************************
******************************************************************************/
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
reset: '\x1b[0m',
yellow: '\x1b[33m',
};
function formatOutput(tree = [], termColors = {}) {
const { yellow: y = '', green: g = '', reset: rst = '' } = termColors;
return tree
.map((member, idx, array) => {
const { length } = array;
const conjunction = idx === length - 1 && length !== 1 ? 'and ' : '';
const last = idx === length - 1;
let ordinal = idx === 0 ? 'parent' : 'grandparent';
if (idx > 1) ordinal = 'great-'.repeat(idx - 1) + ordinal;
let separator = last ? '' : ', ';
if (length === 2) separator = ' ';
return `${conjunction}${y + ordinal + rst} = ${
g + member + rst
}${separator}`;
})
.join('');
}
function assertRoot(tree = [], termColors = {}) {
const last = [...tree].pop();
const {
green: g = '',
red: r = '',
reset: rst = '',
yellow: y = '',
} = termColors;
if (last !== '1/1') {
return `${y}Curious fraction tree rooted at ${
r + last + y
} and not at ${`${g}1/1${rst}`}`;
}
return null;
}
function repl(termColors = {}) {
const { green: g = '', yellow: y = '', reset: rst = '' } = termColors;
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
/* eslint-disable prefer-template */
const mainPrompt =
'Enter a fraction (i.e.: "' +
g +
'3/5' +
rst +
'") or type "' +
y +
'exit' +
rst +
'" to quit /> ';
const morePrompt =
'Enter "' +
y +
'more' +
rst +
'" to see more details, or enter a fraction (i.e.: "' +
g +
'3/5' +
rst +
'") to continue /> ';
/* eslint-enable prefer-template */
let currentTree;
let generations;
let assertedRoot;
rl.setPrompt(mainPrompt);
rl.prompt();
rl.on('line', (line) => {
const input = line.trim().toLowerCase();
if (input === 'exit' || input === 'quit' || input === 'q') {
return rl.close();
}
if (isFractionString(input)) {
currentTree = getCuriousTree(input);
generations = getCuriousGenerations(currentTree, 2);
assertedRoot = assertRoot(currentTree, termColors);
console.log(formatOutput(generations, termColors));
rl.setPrompt(morePrompt);
return rl.prompt();
}
if (input === 'more') {
console.log(formatOutput(currentTree.slice(1), termColors));
if (assertedRoot) console.log(assertedRoot);
currentTree = null;
generations = null;
assertedRoot = null;
rl.setPrompt(mainPrompt);
return rl.prompt();
}
console.log(`Sorry, I don't understand ${input}.`);
return rl.prompt();
}).on('close', () => {
console.log(`${y}Goodbye.${rst}`);
resolve();
});
});
}
function printBanner(termColors = {}) {
const { yellow: y = '', reset: rst = '' } = termColors;
const message = 'A Curious Fraction Tree';
const underline = '='.repeat(message.length);
console.log(`${y}${message}`);
console.log(`${underline}${rst}`);
}
/*******************************************************************************
* Main ************************************************************************
******************************************************************************/
(async function main() {
try {
printBanner(colors);
await repl(colors);
} catch (error) {
console.log(error);
process.exit(1);
}
})();
|