Skip to content

Commit c89cb99

Browse files
committed
feat(permutations): improved variable naming
1 parent f8d9ee9 commit c89cb99

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

16_permutations/solution/permutations-solution.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
const permutations = function (
22
original,
3-
currentPermutations = original.map((num) => [num]),
3+
partialPermutations = original.map((num) => [num]),
44
) {
5-
if (original.length < 2) return [original];
5+
if (original.length <= 1) return [original];
66

7-
const newPerms = [];
8-
currentPermutations.forEach((el) => {
9-
const missing = original.filter((item) => !el.includes(item));
10-
missing.forEach((itemMissing) => newPerms.push([...el, itemMissing]));
7+
const newPartialPermutations = [];
8+
partialPermutations.forEach((partialPermutation) => {
9+
const missingNums = original.filter(
10+
(num) => !partialPermutation.includes(num),
11+
);
12+
missingNums.forEach((missingNum) =>
13+
newPartialPermutations.push([...partialPermutation, missingNum]),
14+
);
1115
});
1216

13-
if (newPerms.every((el) => el.length === original.length)) return newPerms;
14-
return permutations(original, newPerms);
17+
// We can pick any valid index because all of the elements will be the same length
18+
const ANY_INDEX = 0;
19+
20+
if (newPartialPermutations[ANY_INDEX].length === original.length) {
21+
return newPartialPermutations;
22+
}
23+
24+
return permutations(original, newPartialPermutations);
1525
};
1626

1727
// Do not edit below this line

0 commit comments

Comments
 (0)