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

Commite5a06e6

Browse files
committed
Simplify combineWithoutRepetitions function.
1 parent55ecc0b commite5a06e6

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
/**
22
*@param {*[]} comboOptions
33
*@param {number} comboLength
4-
*@param {*[][]} combos
5-
*@param {*[]} currentCombo
64
*@return {*[]}
75
*/
8-
functioncombineRecursively(comboOptions,comboLength,combos=[],currentCombo=[]){
9-
if(comboLength===0){
10-
combos.push(currentCombo);
11-
12-
returncombos;
6+
exportdefaultfunctioncombineWithoutRepetitions(comboOptions,comboLength){
7+
if(comboLength===1){
8+
returncomboOptions.map(comboOption=>[comboOption]);
139
}
1410

11+
constcombos=[];
12+
13+
// Eliminate characters one by one and concatenate them to
14+
// combinations of smaller lengths.s
1515
for(letletterIndex=0;letterIndex<=(comboOptions.length-comboLength);letterIndex+=1){
16-
constletter=comboOptions[letterIndex];
17-
constrestCombinationOptions=comboOptions.slice(letterIndex+1);
16+
constcurrentLetter=comboOptions[letterIndex];
1817

19-
combineRecursively(
20-
restCombinationOptions,
18+
constsmallerCombos=combineWithoutRepetitions(
19+
comboOptions.slice(letterIndex+1),
2120
comboLength-1,
22-
combos,
23-
currentCombo.concat([letter]),
2421
);
22+
23+
smallerCombos.forEach((smallerCombo)=>{
24+
combos.push([currentLetter].concat(smallerCombo));
25+
});
2526
}
2627

2728
returncombos;
2829
}
29-
30-
/**
31-
*@param {*[]} combinationOptions
32-
*@param {number} combinationLength
33-
*@return {*[]}
34-
*/
35-
exportdefaultfunctioncombineWithoutRepetitions(combinationOptions,combinationLength){
36-
returncombineRecursively(combinationOptions,combinationLength);
37-
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp