Java: generator of true false combinations, setting the number N;

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

And here is the task for mathematicians and programmers:

I need to create a method in which I pass an int n parameter:

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

The output should display all possible combinations of true and false.

Here are examples where N = 1; N = 2; N = 3; N = 4; N = 5, where x = false and 0 = true; Please note: empty interrupt lines are just for you to make it easier to recognize patterns. Hope I have 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 

In addition, if you see the conclusion, here is a sign that I recognized that all combinations are inverted in half (for example, the first combination 00000 will be XXXXX last, the second X0000, one to the last - 0XXXX, etc. ...). Perhaps this template will help make the whole algorithm more efficient, not sure about it. Thank you in advance!

+6
source share
7 answers

Here is a really easy way to use only the Java API:

 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 install n at any time convenient for you. And with this result, you can select the character n th from the string as true / false.

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

+5
source

Just tell me, but think about the bits that are set for a number with no more than "n" bits. You will see if you go from 0 to 'n' the number of bits (in this case 3); bits are 000, 001, 010, 011, 100, 101, 110, 111. You can determine the maximum number that can fit in bits n using the formula ((n * n) -1).

+2
source

That 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')); 

of

 000 00X 0X0 0XX X00 X0X XX0 XXX 
+2
source

Here's a simple version implemented using recursion

 public void optionality_generator(int n){ ArrayList<String> strings = generatorHelper(n); for(String s : strings){ System.out.println(s); } } private ArrayList<String> generatorHelper(int n){ if(n == 1){ ArrayList<String> returnVal = new ArrayList<String>(); returnVal.add("0"); returnVal.add("X"); return returnVal; } ArrayList<String> trueStrings = generatorHelper(n-1); for(String s : trueStrings){ s += "0"; } ArrayList<String> falseStrings = generatorHelper(n-1); for(String s : falseStrings){ s += "X"; } trueStrings.addAll(falseStrings); return trueStrings; } 
0
source

Here's the test version:

 import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.List; import org.junit.Test; public class OptionalityTest { @Test public void testOptionality0() throws Exception { assertEquals("[]", optionality(0).toString()); } @Test public void testOptionality1() throws Exception { assertEquals("[0, x]", optionality(1).toString()); } @Test public void testOptionality2() throws Exception { assertEquals("[00, x0, 0x, xx]", optionality(2).toString()); } @Test public void testOptionality3() throws Exception { assertEquals("[000, x00, 0x0, xx0, 00x, x0x, 0xx, xxx]", optionality(3).toString()); } private List<String> optionality(int i) { final ArrayList<String> list = new ArrayList<String>(); if (i == 1) { list.add("0"); list.add("x"); } if (i > 1) { List<String> sublist = optionality(i - 1); for (String s : sublist) { list.add("0" + s); list.add("x" + s); } } return list; } } 
0
source

Below is a modification of the above Ericks code, which uses C # and allows you to enter any number of logical variable names. It will output all possible combinations to C # code, ready for insertion into the if statement. Just edit the first line of code with the names var, and then run LINQpad to get the text output.

Output Example ...

 !VariableNameA && !VariableNameB && !VariableNameC !VariableNameA && !VariableNameB && VariableNameC !VariableNameA && VariableNameB && !VariableNameC !VariableNameA && VariableNameB && VariableNameC VariableNameA && !VariableNameB && !VariableNameC VariableNameA && !VariableNameB && VariableNameC VariableNameA && VariableNameB && !VariableNameC VariableNameA && VariableNameB && VariableNameC 

  //To setup edit var names below string[] varNames = { "VariableNameA", "VariableNameB", "VariableNameC" }; int n = varNames.Count(); for (int i = 0; i < Math.Pow(2, n); i++) { String bin = Convert.ToString(i, 2); while (bin.Length < n) { bin = "0" + bin; } string and = " && "; string f = "!"; string t = " "; var currentNot = bin[0] == '0' ? f : t; //string visual = bin[0].ToString(); string visual = currentNot + varNames[0]; for (var j = 1; j < n; j++) { currentNot = bin[j] == '0' ? f : t; //visual = visual + and + bin[j].ToString(); visual = visual + and + currentNot + varNames[j]; } Console.WriteLine(visual); } 
0
source

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

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

 getBinaryStrings(2, 3); 

For base 3 lines, you call it 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); } } 
0
source

Source: https://habr.com/ru/post/978366/


All Articles