Skip to content

Commit 65f08db

Browse files
committedJun 28, 2018
Simplify combineWithRepetitions function.
1 parent e5a06e6 commit 65f08db

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
/**
2-
* @param {*[]} combinationOptions
3-
* @param {number} combinationLength
2+
* @param {*[]} comboOptions
3+
* @param {number} comboLength
44
* @return {*[]}
55
*/
6-
7-
export default function combineWithRepetitions(combinationOptions, combinationLength) {
8-
// If combination length equal to 0 then return empty combination.
9-
if (combinationLength === 0) {
10-
return [[]];
11-
}
12-
13-
// If combination options are empty then return "no-combinations" array.
14-
if (combinationOptions.length === 0) {
15-
return [];
6+
export default function combineWithRepetitions(comboOptions, comboLength) {
7+
if (comboLength === 1) {
8+
return comboOptions.map(comboOption => [comboOption]);
169
}
1710

1811
// Init combinations array.
1912
const combos = [];
2013

21-
// Find all shorter combinations and attach head to each of those.
22-
const headCombo = [combinationOptions[0]];
23-
const shorterCombos = combineWithRepetitions(combinationOptions, combinationLength - 1);
14+
// Eliminate characters one by one and concatenate them to
15+
// combinations of smaller lengths.
16+
for (let optionIndex = 0; optionIndex < comboOptions.length; optionIndex += 1) {
17+
const currentOption = comboOptions[optionIndex];
2418

25-
for (let combinationIndex = 0; combinationIndex < shorterCombos.length; combinationIndex += 1) {
26-
const combo = headCombo.concat(shorterCombos[combinationIndex]);
27-
combos.push(combo);
28-
}
19+
const smallerCombos = combineWithRepetitions(
20+
comboOptions.slice(optionIndex),
21+
comboLength - 1,
22+
);
2923

30-
// Let's shift head to the right and calculate all the rest combinations.
31-
const combinationsWithoutHead = combineWithRepetitions(
32-
combinationOptions.slice(1),
33-
combinationLength,
34-
);
24+
smallerCombos.forEach((smallerCombo) => {
25+
combos.push([currentOption].concat(smallerCombo));
26+
});
27+
}
3528

36-
// Join all combinations and return them.
37-
return combos.concat(combinationsWithoutHead);
29+
return combos;
3830
}

‎src/algorithms/sets/combinations/combineWithoutRepetitions.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
88
return comboOptions.map(comboOption => [comboOption]);
99
}
1010

11+
// Init combinations array.
1112
const combos = [];
1213

1314
// Eliminate characters one by one and concatenate them to
14-
// combinations of smaller lengths.s
15-
for (let letterIndex = 0; letterIndex <= (comboOptions.length - comboLength); letterIndex += 1) {
16-
const currentLetter = comboOptions[letterIndex];
15+
// combinations of smaller lengths.
16+
for (let optionIndex = 0; optionIndex <= (comboOptions.length - comboLength); optionIndex += 1) {
17+
const currentOption = comboOptions[optionIndex];
1718

1819
const smallerCombos = combineWithoutRepetitions(
19-
comboOptions.slice(letterIndex + 1),
20+
comboOptions.slice(optionIndex + 1),
2021
comboLength - 1,
2122
);
2223

2324
smallerCombos.forEach((smallerCombo) => {
24-
combos.push([currentLetter].concat(smallerCombo));
25+
combos.push([currentOption].concat(smallerCombo));
2526
});
2627
}
2728

0 commit comments

Comments
 (0)
Please sign in to comment.