mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Simplify combineWithRepetitions function.
This commit is contained in:
parent
e5a06e654b
commit
65f08db5de
@ -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);
|
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user