简体   繁体   中英

All Combinations Of Boolean Array

annoyingly this problem is quite easy but has been causing me a little headache today. (Apologies for the pseudocode)

Say we have two array lists of booleans, a and b where:

a = [true, true, false]

b = [true, false, true]

I am trying to generate a list of all combinations of the comparison between a and b with the following rules:

  • If a[i] = true and b[i] = true the resulting result[i] should always returns true .
  • If a[i] = false and b[i] = false always returns false .
  • If a[i] != b[i] returns lists where one result[i] is true and one is false .

So the expected results for compare(a,b) would be ( I hope... ):

[true, true, false],
[true, false, false],
[true, true, true],
[true, false, true]

I have tried to do this in Java but cannot seem to iterate correctly, can anyone give me some help?

EDIT:

Another simple example for rule 3:

a = [true, false]
b = [false, true]

results:

[true, false]
[false, true]
[true, true]
[false, false]

Basically this is what i meant :)

I would say recursion. I don't know what is your goal in performance and how long will be the arrays but I would do something like the following:

1, step : create an array where some elements are fixed and some are marked as ambiguous by your rules, like this:

[Boolean.True, null, null]

This means where is null there should be either true and false.

2, step : in a method search for the last occurence of null , change it to Boolean.True and call the same method with this array. In the next line change it to Boolean.False and call the same method with it again. In this new calls there will be one less null element and you get the array with both Boolean.True and Boolean.False element in the ambiguous space.

3, step : in this method also check whether there is no more null value in the array and write it to the output if there is none.

Of course instead of Boolean and null values you can use something else or more meaningful objects and you can do it in an iterative way also but I don't know the details of your problem.

Hope I could help.

Recursion is the classic way to solve these sort of combinatorial problems.

void resursivelyCombine(List<List<Boolean>> result, List<Boolean> current, List<Boolean> in1, List<Boolean> in2, int index) {
    if (index == in1.size()) {
        result.add(current);
    } else {
        if (in1.get(index).equals(in2.get(index))) {
           current.add(in1.get(index));
           recursivelyCombine(result, current, in1, in2, index+1);
        } else {
           List<Boolean> temp = new ArrayList<>(current);
           temp.add(Boolean.TRUE);
           recursivelyCombine(result, temp, in1, in2, index+1);

           temp = new ArrayList<>(current);
           temp.add(Boolean.FALSE);
           recursivelyCombine(result, temp, in1, in2, index+1);
        }
    }
}

Something like that :) The code will probably need a bit of tidying up as I just typed it in here untested.

Call it like this:

 List<List<Boolean>> results = new ArrayList<>();
 recursivelyCombine(results, new ArrayList<Boolean>(), in1, in2, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM