Add comments to combination algorithms.

This commit is contained in:
Oleksii Trekhleb 2019-01-04 17:03:35 +02:00
parent 6261d0e9bb
commit f08fc37dad
2 changed files with 12 additions and 4 deletions

View File

@ -4,6 +4,8 @@
* @return {*[]} * @return {*[]}
*/ */
export default function combineWithRepetitions(comboOptions, comboLength) { export default function combineWithRepetitions(comboOptions, comboLength) {
// If the length of the combination is 1 then each element of the original array
// is a combination itself.
if (comboLength === 1) { if (comboLength === 1) {
return comboOptions.map(comboOption => [comboOption]); return comboOptions.map(comboOption => [comboOption]);
} }
@ -11,14 +13,16 @@ export default function combineWithRepetitions(comboOptions, comboLength) {
// Init combinations array. // Init combinations array.
const combos = []; const combos = [];
// Eliminate characters one by one and concatenate them to // Remember characters one by one and concatenate them to combinations of smaller lengths.
// combinations of smaller lengths. // We don't extract elements here because the repetitions are allowed.
comboOptions.forEach((currentOption, optionIndex) => { comboOptions.forEach((currentOption, optionIndex) => {
// Generate combinations of smaller size.
const smallerCombos = combineWithRepetitions( const smallerCombos = combineWithRepetitions(
comboOptions.slice(optionIndex), comboOptions.slice(optionIndex),
comboLength - 1, comboLength - 1,
); );
// Concatenate currentOption with all combinations of smaller size.
smallerCombos.forEach((smallerCombo) => { smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo)); combos.push([currentOption].concat(smallerCombo));
}); });

View File

@ -4,6 +4,8 @@
* @return {*[]} * @return {*[]}
*/ */
export default function combineWithoutRepetitions(comboOptions, comboLength) { export default function combineWithoutRepetitions(comboOptions, comboLength) {
// If the length of the combination is 1 then each element of the original array
// is a combination itself.
if (comboLength === 1) { if (comboLength === 1) {
return comboOptions.map(comboOption => [comboOption]); return comboOptions.map(comboOption => [comboOption]);
} }
@ -11,14 +13,16 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
// Init combinations array. // Init combinations array.
const combos = []; const combos = [];
// Eliminate characters one by one and concatenate them to // Extract characters one by one and concatenate them to combinations of smaller lengths.
// combinations of smaller lengths. // We need to extract them because we don't want to have repetitions after concatenation.
comboOptions.forEach((currentOption, optionIndex) => { comboOptions.forEach((currentOption, optionIndex) => {
// Generate combinations of smaller size.
const smallerCombos = combineWithoutRepetitions( const smallerCombos = combineWithoutRepetitions(
comboOptions.slice(optionIndex + 1), comboOptions.slice(optionIndex + 1),
comboLength - 1, comboLength - 1,
); );
// Concatenate currentOption with all combinations of smaller size.
smallerCombos.forEach((smallerCombo) => { smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo)); combos.push([currentOption].concat(smallerCombo));
}); });