/
app.js
54 lines (49 loc) · 1.28 KB
/
app.js
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
const { Console } = require("./console");
const console = new Console();
let data = readData();
console.writeln(printRows(`Permutación`, getPermutations(data)));
function readData() {
let data = [];
let i = 0;
let end;
do {
const answer = console.readString(`Dame un valor o un punto "." para finalizar la serie: `);
end = answer === `.`;
if (!end) {
data[i] = answer;
i++;
}
} while (!end);
return data;
}
function printRows(title, [head, ...tail], index = 0) {
if (head === undefined){
return ``;
}
return printRow(`${title}-${index}`, head) + printRows(title, tail, index+1);
}
function printRow(title, [head, ...tail], index = 0) {
if (head === undefined){
return `\n`;
}
return `${index === 0? `${title}:` : ``} ${head} ` + printRow(title, tail, index + 1);
}
function getPermutations(data) {
if (data.length === 0){
return [];
}
if (data.length === 1){
return [data];
}
var result = [];
for(let pivot=0; pivot<data.length; pivot++){
let withoutPivot = data.slice();
withoutPivot.splice(pivot,1);
let permutations = getPermutations(withoutPivot);
for(let i=0; i<permutations.length; i++){
permutations[i].push(data[pivot]);
}
result = result.concat(permutations);
}
return result;
}