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

后端 未结 7 2269
野的像风
野的像风 2020-12-09 13:53

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

相关标签:
7条回答
  • 2020-12-09 14:00

    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 讨论(0)
  • 2020-12-09 14:06

    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.

    0 讨论(0)
  • 2020-12-09 14:07

    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
    
    0 讨论(0)
  • 2020-12-09 14:12

    Here is a modification from Erics code above, that uses c# and allows input of any number of boolean variable names. It will output all possible combinations in c# code ready for insert into an if statement. Just edit the 1st line of code with var names, and then run in LINQpad to get a 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 讨论(0)
  • 2020-12-09 14:13

    Here's a test-driven 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 讨论(0)
  • 2020-12-09 14:17

    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.

    0 讨论(0)
提交回复
热议问题