Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf08fc37

Browse files
committed
Add comments to combination algorithms.
1 parent6261d0e commitf08fc37

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

‎src/algorithms/sets/combinations/combineWithRepetitions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
*@return {*[]}
55
*/
66
exportdefaultfunctioncombineWithRepetitions(comboOptions,comboLength){
7+
// If the length of the combination is 1 then each element of the original array
8+
// is a combination itself.
79
if(comboLength===1){
810
returncomboOptions.map(comboOption=>[comboOption]);
911
}
1012

1113
// Init combinations array.
1214
constcombos=[];
1315

14-
//Eliminate characters one by one and concatenate them to
15-
//combinations of smaller lengths.
16+
//Remember characters one by one and concatenate them to combinations of smaller lengths.
17+
//We don't extract elements here because the repetitions are allowed.
1618
comboOptions.forEach((currentOption,optionIndex)=>{
19+
// Generate combinations of smaller size.
1720
constsmallerCombos=combineWithRepetitions(
1821
comboOptions.slice(optionIndex),
1922
comboLength-1,
2023
);
2124

25+
// Concatenate currentOption with all combinations of smaller size.
2226
smallerCombos.forEach((smallerCombo)=>{
2327
combos.push([currentOption].concat(smallerCombo));
2428
});

‎src/algorithms/sets/combinations/combineWithoutRepetitions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
*@return {*[]}
55
*/
66
exportdefaultfunctioncombineWithoutRepetitions(comboOptions,comboLength){
7+
// If the length of the combination is 1 then each element of the original array
8+
// is a combination itself.
79
if(comboLength===1){
810
returncomboOptions.map(comboOption=>[comboOption]);
911
}
1012

1113
// Init combinations array.
1214
constcombos=[];
1315

14-
//Eliminate characters one by one and concatenate them to
15-
//combinations of smaller lengths.
16+
//Extract characters one by one and concatenate them to combinations of smaller lengths.
17+
//We need to extract them because we don't want to have repetitions after concatenation.
1618
comboOptions.forEach((currentOption,optionIndex)=>{
19+
// Generate combinations of smaller size.
1720
constsmallerCombos=combineWithoutRepetitions(
1821
comboOptions.slice(optionIndex+1),
1922
comboLength-1,
2023
);
2124

25+
// Concatenate currentOption with all combinations of smaller size.
2226
smallerCombos.forEach((smallerCombo)=>{
2327
combos.push([currentOption].concat(smallerCombo));
2428
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp