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

Commit65f08db

Browse files
committed
Simplify combineWithRepetitions function.
1 parente5a06e6 commit65f08db

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed
Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
/**
2-
*@param {*[]}combinationOptions
3-
*@param {number}combinationLength
2+
*@param {*[]}comboOptions
3+
*@param {number}comboLength
44
*@return {*[]}
55
*/
6-
7-
exportdefaultfunctioncombineWithRepetitions(combinationOptions,combinationLength){
8-
// If combination length equal to 0 then return empty combination.
9-
if(combinationLength===0){
10-
return[[]];
11-
}
12-
13-
// If combination options are empty then return "no-combinations" array.
14-
if(combinationOptions.length===0){
15-
return[];
6+
exportdefaultfunctioncombineWithRepetitions(comboOptions,comboLength){
7+
if(comboLength===1){
8+
returncomboOptions.map(comboOption=>[comboOption]);
169
}
1710

1811
// Init combinations array.
1912
constcombos=[];
2013

21-
// Find all shorter combinations and attach head to each of those.
22-
constheadCombo=[combinationOptions[0]];
23-
constshorterCombos=combineWithRepetitions(combinationOptions,combinationLength-1);
14+
// Eliminate characters one by one and concatenate them to
15+
// combinations of smaller lengths.
16+
for(letoptionIndex=0;optionIndex<comboOptions.length;optionIndex+=1){
17+
constcurrentOption=comboOptions[optionIndex];
2418

25-
for(letcombinationIndex=0;combinationIndex<shorterCombos.length;combinationIndex+=1){
26-
constcombo=headCombo.concat(shorterCombos[combinationIndex]);
27-
combos.push(combo);
28-
}
19+
constsmallerCombos=combineWithRepetitions(
20+
comboOptions.slice(optionIndex),
21+
comboLength-1,
22+
);
2923

30-
// Let's shift head to the right and calculate all the rest combinations.
31-
constcombinationsWithoutHead=combineWithRepetitions(
32-
combinationOptions.slice(1),
33-
combinationLength,
34-
);
24+
smallerCombos.forEach((smallerCombo)=>{
25+
combos.push([currentOption].concat(smallerCombo));
26+
});
27+
}
3528

36-
// Join all combinations and return them.
37-
returncombos.concat(combinationsWithoutHead);
29+
returncombos;
3830
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ export default function combineWithoutRepetitions(comboOptions, comboLength) {
88
returncomboOptions.map(comboOption=>[comboOption]);
99
}
1010

11+
// Init combinations array.
1112
constcombos=[];
1213

1314
// Eliminate characters one by one and concatenate them to
14-
// combinations of smaller lengths.s
15-
for(letletterIndex=0;letterIndex<=(comboOptions.length-comboLength);letterIndex+=1){
16-
constcurrentLetter=comboOptions[letterIndex];
15+
// combinations of smaller lengths.
16+
for(letoptionIndex=0;optionIndex<=(comboOptions.length-comboLength);optionIndex+=1){
17+
constcurrentOption=comboOptions[optionIndex];
1718

1819
constsmallerCombos=combineWithoutRepetitions(
19-
comboOptions.slice(letterIndex+1),
20+
comboOptions.slice(optionIndex+1),
2021
comboLength-1,
2122
);
2223

2324
smallerCombos.forEach((smallerCombo)=>{
24-
combos.push([currentLetter].concat(smallerCombo));
25+
combos.push([currentOption].concat(smallerCombo));
2526
});
2627
}
2728

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp