Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8d3f83c

Browse files
committed
Simplify combineWithoutRepetition algorithm.
1 parent c3a9618 commit 8d3f83c

File tree

1 file changed

+25
-56
lines changed

1 file changed

+25
-56
lines changed
Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,37 @@
1-
/*
2-
@see: https://stackoverflow.com/a/127898/7794070
3-
4-
Lets say your array of letters looks like this: "ABCDEFGH".
5-
You have three indices (i, j, k) indicating which letters you
6-
are going to use for the current word, You start with:
7-
8-
A B C D E F G H
9-
^ ^ ^
10-
i j k
11-
12-
First you vary k, so the next step looks like that:
13-
14-
A B C D E F G H
15-
^ ^ ^
16-
i j k
17-
18-
If you reached the end you go on and vary j and then k again.
19-
20-
A B C D E F G H
21-
^ ^ ^
22-
i j k
1+
/**
2+
* @param {*[]} comboOptions
3+
* @param {number} comboLength
4+
* @param {*[][]} combos
5+
* @param {*[]} currentCombo
6+
* @return {*[]}
7+
*/
8+
function combineRecursively(comboOptions, comboLength, combos = [], currentCombo = []) {
9+
if (comboLength === 0) {
10+
combos.push(currentCombo);
2311

24-
A B C D E F G H
25-
^ ^ ^
26-
i j k
12+
return combos;
13+
}
2714

28-
Once you j reached G you start also to vary i.
15+
for (let letterIndex = 0; letterIndex <= (comboOptions.length - comboLength); letterIndex += 1) {
16+
const letter = comboOptions[letterIndex];
17+
const restCombinationOptions = comboOptions.slice(letterIndex + 1);
2918

30-
A B C D E F G H
31-
^ ^ ^
32-
i j k
19+
combineRecursively(
20+
restCombinationOptions,
21+
comboLength - 1,
22+
combos,
23+
currentCombo.concat([letter]),
24+
);
25+
}
3326

34-
A B C D E F G H
35-
^ ^ ^
36-
i j k
37-
...
38-
*/
27+
return combos;
28+
}
3929

4030
/**
4131
* @param {*[]} combinationOptions
4232
* @param {number} combinationLength
4333
* @return {*[]}
4434
*/
4535
export default function combineWithoutRepetitions(combinationOptions, combinationLength) {
46-
// If combination length is just 1 then return combinationOptions.
47-
if (combinationLength === 1) {
48-
return combinationOptions.map(option => [option]);
49-
}
50-
51-
// Init combinations array.
52-
const combinations = [];
53-
54-
for (let i = 0; i <= (combinationOptions.length - combinationLength); i += 1) {
55-
const smallerCombinations = combineWithoutRepetitions(
56-
combinationOptions.slice(i + 1),
57-
combinationLength - 1,
58-
);
59-
60-
for (let j = 0; j < smallerCombinations.length; j += 1) {
61-
const combination = [combinationOptions[i]].concat(smallerCombinations[j]);
62-
combinations.push(combination);
63-
}
64-
}
65-
66-
// Return all calculated combinations.
67-
return combinations;
36+
return combineRecursively(combinationOptions, combinationLength);
6837
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /