Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Generator of true's & false's combinations by giving the number N;

I tied to simplify the task as much as possible, so I could apply it to my algorithm.

And here is the challenge for mathematicians and programmers:

I need to create a method where I pass parameter int n:

public void optionality_generator(int n){
  //some kind of loops, or recursions...to make it workable
  System.out.println("current combination: ...");
}

The output should show all possible combinations of true's and false's.

Here is examples where N=1; N=2; N=3; N=4; N=5 where x=false and 0=true; Please note, empty break lines is just for you to recognise easier the patterns. Hopefully, I included all possible combinations):

Combination of 1:
0
x

Combination of 2:
00
x0
0x
xx

Combination of 3:
000
X00
0X0
00X
XX0
0XX
XXX

Combination of 4:
0000

X000
0X00
00X0
000X

XX00
X0X0
X00X

0XX0
0X0X

00XX

XXX0
XX0X
X0XX
0XXX

XXXX

Combination of 5:
00000
X0000
0X000
00X00
000X0
0000X

XX000
X0X00
X00X0
X000X

X0X00
X00X0
X000X

0XX00
0X0X0
0X00X

00XX0
00X0X

000XX

XXX00
XX0X0
XX00X

X0XX0
X0X0X
X00XX

0XXX0
0XX0X

00XXX

XXXX0
XXX0X
XX0XX
X0XXX
0XXXX

XXXXX

Also, If you see the output, here is the pattern I recognized, that all combinations are inverted on half (e.g first combination is 00000 last one will be XXXXX, second one X0000, one before the last one will be 0XXXX etc..). Maybe, this pattern will help to make the whole algorithm more efficient, not sure about this. Thank you in advance!

like image 491
vvinjj Avatar asked Jun 06 '12 23:06

vvinjj


4 Answers

Here is a really basic way using only Java APIs:

final int n = 3;
for (int i = 0; i < Math.pow(2, n); i++) {
    String bin = Integer.toBinaryString(i);
    while (bin.length() < n)
        bin = "0" + bin;
    System.out.println(bin);
}

Result:

000
001
010
011
100
101
110
111

Of course, you can set n to whatever you like. And, with this result, you can pick the nth character from the string as true/false.

If you only need to check if a bit is true, you don't need to convert it to a string. This is just to illustrate the output values.

like image 54
Cat Avatar answered Nov 03 '22 14:11

Cat


Just a clue but think about the bits that are set for a number with at most 'n' bits. You'll see if you go from 0 to 'n' number of bits (3 in this case); the bits are 000, 001, 010, 011, 100, 101, 110, 111. You can figure out the max number that can fit in 'n' bits by using the ((n*n)-1) formula.

like image 2
Justin Avatar answered Nov 03 '22 14:11

Justin


This should do the trick

int cols = 3;
int rows = (int) Math.pow(2, cols);
for (int row = 0; row < rows; row++)
    System.out.println(String.format("%" + cols + "s", 
            Integer.toBinaryString(row)).replace(' ', '0').replace('1', 'X'));

out:

000
00X
0X0
0XX
X00
X0X
XX0
XXX
like image 2
Pshemo Avatar answered Nov 03 '22 14:11

Pshemo


Using recursion is not as easy as using the Java Integer.toBinaryString() API for generating binary strings. But the code below gives you the flexibility to generate any base representation, e.g. base 3: "000" "001" "002" "010" "011" "012"

For base 2 (i.e. binary) strings, you call it like this:

getBinaryStrings(2, 3);

For base 3 strings, you call it like this:

getBinaryStrings(3, 3);

Here is the code:

public static List<String> getBinaryStrings(int base, int n){
    ArrayList<String> result = new ArrayList<>();
    getBinaryStringsCore(base, n, "", result);
    return result;
}

private static void getBinaryStringsCore(int base, int n, String tempString, List<String> result){
    if (tempString.length() == n) {
        result.add(tempString);
        return;
    }

    for (int i = 0; i < base; i++) {
        tempString += i;
        getBinaryStringsCore(base, n, tempString, result);
        tempString = tempString.substring(0, tempString.length() - 1);
    }
}
like image 1
David Long Avatar answered Nov 03 '22 13:11

David Long