Simplify combineWithRepetitions function.

This commit is contained in:
Oleksii Trekhleb 2018-06-28 14:05:58 +03:00
parent e5a06e654b
commit 65f08db5de
2 changed files with 24 additions and 31 deletions

View File

@ -1,38 +1,30 @@
/** /**
* @param {*[]} combinationOptions * @param {*[]} comboOptions
* @param {number} combinationLength * @param {number} comboLength
* @return {*[]} * @return {*[]}
*/ */
export default function combineWithRepetitions(comboOptions, comboLength) {
export default function combineWithRepetitions(combinationOptions, combinationLength) { if (comboLength === 1) {
// If combination length equal to 0 then return empty combination. return comboOptions.map(comboOption => [comboOption]);
if (combinationLength === 0) {
return [[]];
}
// If combination options are empty then return "no-combinations" array.
if (combinationOptions.length === 0) {
return [];
} }
// Init combinations array. // Init combinations array.
const combos = []; const combos = [];
// Find all shorter combinations and attach head to each of those. // Eliminate characters one by one and concatenate them to
const headCombo = [combinationOptions[0]]; // combinations of smaller lengths.
const shorterCombos = combineWithRepetitions(combinationOptions, combinationLength - 1); for (let optionIndex = 0; optionIndex < comboOptions.length; optionIndex += 1) {
const currentOption = comboOptions[optionIndex];
for (let combinationIndex = 0; combinationIndex < shorterCombos.length; combinationIndex += 1) { const smallerCombos = combineWithRepetitions(
const combo = headCombo.concat(shorterCombos[combinationIndex]); comboOptions.slice(optionIndex),
combos.push(combo); comboLength - 1,
);
smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo));
});
} }
// Let's shift head to the right and calculate all the rest combinations. return combos;
const combinationsWithoutHead = combineWithRepetitions(
combinationOptions.slice(1),
combinationLength,
);
// Join all combinations and return them.
return combos.concat(combinationsWithoutHead);
} }

View File

@ -8,20 +8,21 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
return comboOptions.map(comboOption => [comboOption]); return comboOptions.map(comboOption => [comboOption]);
} }
// Init combinations array.
const combos = []; const combos = [];
// Eliminate characters one by one and concatenate them to // Eliminate characters one by one and concatenate them to
// combinations of smaller lengths.s // combinations of smaller lengths.
for (let letterIndex = 0; letterIndex <= (comboOptions.length - comboLength); letterIndex += 1) { for (let optionIndex = 0; optionIndex <= (comboOptions.length - comboLength); optionIndex += 1) {
const currentLetter = comboOptions[letterIndex]; const currentOption = comboOptions[optionIndex];
const smallerCombos = combineWithoutRepetitions( const smallerCombos = combineWithoutRepetitions(
comboOptions.slice(letterIndex + 1), comboOptions.slice(optionIndex + 1),
comboLength - 1, comboLength - 1,
); );
smallerCombos.forEach((smallerCombo) => { smallerCombos.forEach((smallerCombo) => {
combos.push([currentLetter].concat(smallerCombo)); combos.push([currentOption].concat(smallerCombo));
}); });
} }