I have 3, js functions:
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
}
I need to find all possible combinations of these functions up to two functions in a string that is, it need to return the below strings, in form of array
"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
Notes:
$
represents some value that need not be interfered in this Q.s, so let $ ,be $ for this Q.s- the three js functions may vary to any extent, in my case 7 js functions, with six of them having two inputs ,and one function with one input
- up to two functions in a string may vary to any extent, like up to 5 functions...
I have learnt to generate possible combinations ,as shown below
/*
* 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);
Its is a modification to this blog,
But I don't know how to implement it with a string that is in a form of function with brackets,
I guess using xml will help as shown in here
I have practice of using xml for data and algorithm handling in js,
But still I don't know how to implement it.,Please help