Simplify permutateWithRepetitions algorithm.

This commit is contained in:
Oleksii Trekhleb
2018-06-28 21:28:50 +03:00
parent c5ed81d85e
commit db7ab9e299
6 changed files with 24 additions and 135 deletions

View File

@@ -13,9 +13,7 @@ export default function combineWithRepetitions(comboOptions, comboLength) {
// Eliminate characters one by one and concatenate them to
// combinations of smaller lengths.
for (let optionIndex = 0; optionIndex < comboOptions.length; optionIndex += 1) {
const currentOption = comboOptions[optionIndex];
comboOptions.forEach((currentOption, optionIndex) => {
const smallerCombos = combineWithRepetitions(
comboOptions.slice(optionIndex),
comboLength - 1,
@@ -24,7 +22,7 @@ export default function combineWithRepetitions(comboOptions, comboLength) {
smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo));
});
}
});
return combos;
}

View File

@@ -13,9 +13,7 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
// Eliminate characters one by one and concatenate them to
// combinations of smaller lengths.
for (let optionIndex = 0; optionIndex <= (comboOptions.length - comboLength); optionIndex += 1) {
const currentOption = comboOptions[optionIndex];
comboOptions.forEach((currentOption, optionIndex) => {
const smallerCombos = combineWithoutRepetitions(
comboOptions.slice(optionIndex + 1),
comboLength - 1,
@@ -24,7 +22,7 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo));
});
}
});
return combos;
}