-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations.js
30 lines (24 loc) · 1.01 KB
/
permutations.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
// You have an array of lists e.g.; [[1,3], ['a'], [4,5]].
// We would like to obtain all the permutations between the lists.
// The answer for this example is: (1,a,4) (1,a,5) (3,a,4) (3,a,5).
// Code a program that does this for any quantity of lists and elements on them.
function permutations(arr) {
if (Array.isArray(arr)) {
if(arr.length < 2) return arr;
const arrayOfArrays = arr.reduce((acc, elAsArr) => {
const firstStep = acc.map((accElAsArr) => elAsArr.map((el) => accElAsArr.concat(el)));
const secondStep = firstStep.reduce((prev, curr) => [...prev, ...curr], []);
return secondStep;
}, [[]]);
const arrayOfResp = arrayOfArrays.map((el) => {
const arrAsStr = el.join(',');
return arrAsStr
.padStart(arrAsStr.length + 1, '(')
.padEnd(arrAsStr.length + 2, ')')
});
const resp = arrayOfResp.join('');
return resp;
}
return 'This function expect an array.';
}
permutations([[1, 3], ['a'], [4, 5]]); // => "(1,a,4)(1,a,5)(3,a,4)(3,a,5)"