Results 1 to 10 of 10
- 06-18-2010, 07:30 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
All possible combinations given a binary number
Hi all,
I have a 2D array which represents the binary numbers.
Given a 2D array of [count][posvalues], count refers to the number of binary, and posvalues refer to the possible values of each binary.
Ex. binary num: [0, 1] [0,1] [0,1]
Count = 3
Possible Values = 2 (given by 0 and 1)
I want to find all the possible combinations of everything in the array using a Recursive solution, but I'm having a hard time.
Please help me, it's driving me crazy!
Thank you!
- 06-18-2010, 07:41 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 06-18-2010, 07:43 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Hi JosAH, thanks for replying.
Thank you for that, although to confirm, I am looking for the 000, 001, 010, ..., etc. I want to print those possible combinations.
Thank you again.
- 06-18-2010, 07:48 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 06-18-2010, 07:52 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Hi JosAH, thanks again for replying.
Yes, I want them all. For example, I have count == 3;
Then my wanted output would be:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
I am having a hard time implementing it on the 2D array specified above. The reason that I am using a 2D array is because the values may not just be (0 and 1). It can have 2 values. For example: n = 2, and possible values = 0, 1, 2
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
Or something like that.
Thanks again.Last edited by LeanA; 06-18-2010 at 07:55 AM.
- 06-18-2010, 08:19 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
It's still just counting given a certain radix; e.g. suppose you have the number 012 and the allowed values are 0, 1, and 2; you start from the right and check if the digit < 2; if not reset the digit and move to the left; if so, increment the digit and stop; so 012 --> 020. The following example illustrates the algorithm:
kind regards,Java Code:import java.util.Arrays; public class Combination { public static int[] next(int[] current, int radix) { int[] n= new int[current.length]; for (int i= n.length; i-- > 0;) { if (current[i]+1 == radix) n[i]= 0; else { n[i]= current[i]+1; for (; i-- > 0; n[i]= current[i]); return n; } } return null; } public static void main(String[] args) { int N= 4, M= 3; // values 0, 1, 2, 3 (<N), we want M=3 of them int[] a= new int[M]; do { System.out.println(Arrays.toString(a)); } while ((a= next(a, N)) != null); } }
Jos
- 06-18-2010, 08:57 AM #7
Crap, someone beat me to it. I have a bit of semi-pseudocode that I threw together using Python as a test base that you might find helpful. Just putting this here so that my staying up till 1:30am wasn't in vain!
Best of luck to you!Java Code:public void printcombos(int n, int[] posvalues, int base, int[] values) { // N is your count; // Posvalues is an int array of possible values; // Base is the base counter for the posvalues index to display; start this at zero; // Values is an array of values to display; start this as blank {}. for (int i = 0; i < posvalues.length; i++) { newvals = arraycopy(values); newvals.push(posvalues[i]); if (base <= len(posvalues)) { printcombos(n,posvalues,base + 1,newvals); } if (newvals.length == n) { System.out.println(Array.toString(newvals)); } } } // Call this function using: // printcombos(2,{0,1,2},0,{});
(PS: That's quite a bit of code, Jos. Top marks. Not sure that the do/while counts as recursion, though...)Last edited by Zack; 06-18-2010 at 08:58 AM. Reason: Added my PS! ;)
- 06-18-2010, 09:04 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 06-18-2010, 09:16 AM #9
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Hi JosAH and Zack,
Thank you for those code. Although a little different from what I am aiming for, I think that I can try to modify those.
What I really am aiming for is something like this (inputting the 2D array to the function):
Thank you very much for your help!Java Code:public static void PrintPossibleKeys(String[][] possiblecombo) { //Recursively iterate through the 2D array then print every possible combination }
- 06-18-2010, 05:33 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Hi all,
I finally solved this problem. Here is the recursive printing of the values:
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.Java Code: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[i][b]); } } }
Thank you all!
Similar Threads
-
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02-15-2010, 12:37 AM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
Geting Mobile Number, Mobile Operator, Location and Mobile Serial Number by J2ME.
By maruffaiz in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 12:14 PM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
Converts a binary number to a decimal
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 09:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks