mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add comments to combination algorithms.
This commit is contained in:
parent
6261d0e9bb
commit
f08fc37dad
@ -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));
|
||||||
});
|
});
|
||||||
|
@ -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));
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user