我有3個,js函式:
function f1(a,b){
return 0;//returns something
}
function f2(a,b){
return 0;//returns something
}
function f3(a){/*note that this has only 1 input: a*/
return 0;//returns something
}
我需要在一個字串中找到這些函式的所有可能組合,最多兩個函式,也就是說,它需要以陣列的形式回傳以下字串
"f1(f1($,$),$)"//used functions:2:- f1,f1
"f1($,f1($,$))"//used functions:2:- f1,f1
"f1(f2($,$),$)"//used functions:2:- f1,f2
"f1($,f2($,$))"//used functions:2:- f1,f2
"f1(f3($),$)"//used functions:2:- f1,f3
"f1($,f3($))"//used functions:2:- f1,f3
"f1($,$)"//used functions:1:- f1
"f2(f1($,$),$)"//used functions:2:- f2,f1
"f2($,f1($,$))"//used functions:2:- f2,f1
"f2(f2($,$),$)"//used functions:2:- f2,f2
"f2($,f2($,$))"//used functions:2:- f2,f2
"f2(f3($),$)"//used functions:2:- f2,f3
"f2($,f3($))"//used functions:2:- f2,f3
"f2($,$)"//used functions:1:- f2
"f3(f1($,$))"//used functions:2:- f3,f1
"f3(f2($,$))"//used functions:2:- f3,f2
"f3(f3($))"//used functions:2:- f3,f3
"f3($)"//used functions:1:- f3
筆記:
$表示在這個 Qs 中不需要干涉的一些值,所以讓$ , 成為這個 Qs 的$- 這三個 js 函式可能會有所不同,在我的例子中是 7 個 js 函式,其中六個有 兩個 輸入,一個函式有 一個 輸入
- 一個字串中最多兩個函式可能會有所不同,例如最多 5 個函式...
我已經學會了生成可能的組合,如下所示
/*
* Generate all possible combinations from a list of characters for a given length
*/
function* charCombinations(chars, minLength, maxLength) {
chars = typeof chars === 'string' ? chars : '';
minLength = parseInt(minLength) || 0;
maxLength = Math.max(parseInt(maxLength) || 0, minLength);
//Generate for each word length
for (i = minLength; i <= maxLength; i ) {
//Generate the first word for the combination length by the repetition of first character.
word = (chars[0] || '').repeat(i);
yield word;
//Generate other possible combinations for the word
//Total combinations will be chars.length raised to power of word.length
//Make iteration for all possible combinations
for (j = 1; j < Math.pow(chars.length, i); j ) {
//Make iteration for all indices of the word
for (k = 0; k < i; k ) {
//check if the current index char need to be flipped to the next char.
if (!(j % Math.pow(chars.length, k))) {
// Flip the current index char to the next.
let charIndex = chars.indexOf(word[k]) 1;
char = chars[charIndex < chars.length ? charIndex : 0];
word = word.substr(0, k) char word.substr(k char.length);
}
}
//Re-oder not neccesary but it makes the words are yeilded alphabetically on ascending order.
yield word.split('').reverse().join('');
}
}
}
let combinations = charCombinations('abc', 1, 3);
let combination = 0;
var carray = [];
while (typeof combination != "undefined") {
combination = combinations.next().value
carray.push(combination);
}
carray.pop();
console.log(carray);
它是對這個博客的修改,
但我不知道如何用帶括號的函式形式的字串來實作它,我想使用 xml 會有所幫助,如此處
所示
我有使用 xml 資料和js中的演算法處理,
但是我還是不知道怎么實作,請幫忙
uj5u.com熱心網友回復:
您可以使用接受以下輸入的遞回生成器:
帶有引數占位符的函式呼叫字串陣列,
$例如:["f1($,$)", "f2($,$)", "f3($)"]. 這意味著您可以控制一個函式需要多少個引數,以及有多少只需要 1(在您的問題中始終為 1),或 2,甚至更多。每個輸出生成的最小函式呼叫數(在您的問題中這似乎總是 1)
每個輸出生成的最大函式呼叫數(如 7 個)
該演算法將當前字串傳遞給遞回呼叫,遞回呼叫可以進一步擴展它,直到產生它。這個想法是每個$都在字串中找到,并且可以用更多的嵌套函式呼叫替換。
片段:
function* combinations(funcs, minCount, maxCount, result="$", start=0) {
if (maxCount <= 0) return yield result;
for (let f of funcs) {
for (let i = result.indexOf("$", start); i >= 0; i = result.indexOf("$", i 1)) {
yield* combinations(funcs, minCount - 1, maxCount - 1, result.slice(0, i) f result.slice(i 1), i);
}
}
if (minCount <= 0) yield result;
}
// Example run
for (let s of combinations(["f1($,$)", "f2($,$)", "f3($)"], 1, 2)) {
console.log(s);
}
如果您的輸入只是函式物件(參考),那么您需要進行一些預處理以獲取它們的名稱和定義它們的引數數量:
function describe(f) {
return `${f.name}(${Array(f.length).fill("$").join(",")})`;
}
function* combinations(funcs, minCount, maxCount, result="$", start=0) {
if (maxCount <= 0) return yield result;
for (let f of funcs) {
for (let i = result.indexOf("$", start); i >= 0; i = result.indexOf("$", i 1)) {
yield* combinations(funcs, minCount - 1, maxCount - 1, result.slice(0, i) f result.slice(i 1), i);
}
}
if (minCount <= 0) yield result;
}
// Example run
function f1(a,b){ return 0; }
function f2(a,b){ return 0; }
function f3(a){ return 0; }
for (let s of combinations([f1, f2, f3].map(describe), 1, 2)) {
console.log(s);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492034.html
標籤:javascript 算法
上一篇:分而治之與回溯
下一篇:橢圓的邊在大位圖上被切割?
