Waterborne wrote:Permutations.
I want to find out all the possible combinations of it.
I currently am using recursion however, I am having difficulty in finding the correct way to do it.Can't say I can help unless you explain to us the difficulties your facing, possibly post your initial attempts.
Melanie_Green wrote:At least the example shows combinations. The are 2^3 = 8 of them. If you look at the second index on each row, it goes like this,Waterborne wrote:Permutations.
I want to find out all the possible combinations of it.
Waterborne wrote:I suggest you first develop an iterative solution based on nested loops. Each position will be one nesting and the loops will iterate the number of values. Then you generalize this to a recursive solution.
Hi Melanie and Wastrel, thanks for replying:
Yes, I am using recursion since the num of items and possible values may change.
int m = 0;
for(int a = 0; a < index; a++)
{
for(int b = 0; b < index; b++)
{
for(int c = 0; c < index; c++)
{
System.out.println("TEST: " + possibledecrypted[m][a] + " " + possibledecrypted[m+1] + " " + possibledecrypted[m+2][c]);
}
}
}
I picture this in recursion as:
String PrintKey(String[][] possibledecrypted, int m, int maxnumber, int index)
{
if(m == maxnumber)
{
return possibledecrypted[m][curindex];
}
else
{
for(int m = 0; m < index; m++)
{
System.out.println("TEST: " + possibledecrypted[m][a] + " " + PrintKeyAgain(possibledecrypted, m + 1, maxnumber, index));
}
}
}
Problem is, the above code does not work properly at all. It does not get all the proper combinations. Also, it just prints 2 number of items.
Please help me, recursions always blow my head off!
Thank you!
public static void PrintPossibleKeys(String[][] possibledecrypted, int i, int j, int max, int index, String curstring)
{
if((i+1) == max)
{
for(int c = 0; c < index; c++)
{
System.out.println("Test: " + curstring + " " + possibledecrypted[i][c]);
}
}
else
{
for(int b = 0; b < index; b++)
{
PrintPossibleKeys(possibledecrypted, (i+1), 0, max, index, (curstring) + " " + possibledecrypted);
}
}
}
The main idea I used is to collectively collect the strings traversed and pass it as a parameter then print everything out once it reaches the final.
Special thanks to wastrel for giving me tips on how to create recursions (first try iterative solution).
Thank you all!