0

I create a script that displays the results depending on the selected options.

I use: A, B, C, D, E, F, G, H (in the future will be more)

int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;
int F = 0;
int G = 0;
int H = 0;

final ansver will be one of 16 combination.

A;C;E;G = Nr1
A;C;E;H = Nr2
A;C;F;G = Nr3
A;C;F;H = Nr4
A;D;E;G = Nr5
A;D;E;H = Nr6
A;D;F;G = Nr7
A;D;F;H = Nr8
B;C;E;G = Nr9
B;C;E;H = Nr10
B;C;F;G = Nr11
B;C;F;H = Nr12
B;D;E;G = Nr13
B;D;E;H = Nr14
B;D;F;G = Nr15
B;D;F;H = Nr16

I want to show the potential variants.
When you press the combination of: A, C' then answer is '"Nr1,Nr2,Nr3,Nr4"
When you press the combination of: A, G' then answer is '"Nr1,Nr3,Nr5,Nr7"
Later will be more variables I, J, K, L .... etc. But the answers will be only 16th

What could be the logic for A data structure, such as a Map, I'm a little stuck?

Important - combinations can be created also mix cases for example: H;C;E;A or E;C;A;H .... etc. answer will be Nr1

If / Else seems to be too long. currently code:

    String scoreTeamA = "waiting for the results";


    if (A == 1) {
        if (C == 1) {
            if (E == 1) {
                if (G == 1) {
                    scoreTeamA = "The answer is: Nr1"; //combination: A;C;E;G
                } else if (H == 1) {
                    scoreTeamA = "The answer is: Nr2"; //combination: A;C;E;H
                } else scoreTeamA = "Possible variants, one of: Nr1, Nr2"; //combination: A;C;E

            } else if (F == 1) {
                if (G == 1) {
                    scoreTeamA = "The answer is: Nr3"; //combination: A;C;F;G
                } else if (H == 1) {
                    scoreTeamA = "The answer is: Nr4"; //combination: A;C;F;H
                } else scoreTeamA = "Possible variants, one of: Nr3,Nr4"; //combination: A;C;F

            } else scoreTeamA = "Possible variants, one of: Nr1,Nr2,Nr3,Nr4"; //combination: A;C;

        } else if (D == 1) {
            scoreTeamA = "Possible variants, one of: Nr5,Nr6,Nr7,Nr8";//combination: A;D;
        } else
            scoreTeamA = "Possible variants, one of: Nr1,Nr2,Nr3,Nr4,Nr5,Nr6,Nr7,Nr8"; //combination: A
    } else if (B == 1) { 
        scoreTeamA = "Possible variants, one of: Nr9,Nr10,Nr11,Nr12,Nr13,Nr14,Nr15,Nr16"; //combination: B
    }
  • I see four binary variables. Use a Tree. – Compass Feb 22 '17 at 15:26
  • @HypnicJerk and @Compass I can shorten the number of variables; to 4 (`A, B, C, D`) then I get each of these elements is 3 values `-1; 0; 1` – Mārtiņš Doniņš Feb 22 '17 at 15:39
  • Maybe a [`BitSet`](http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html) as the representation for any combination of the letters will help here. You could then use a `Map` for getting the answers. – Seelenvirtuose Feb 22 '17 at 15:59
  • 1
    @Seelenvirtuose it won't work, not in a straightforward manner at least. Read the question: incomplete inputs should produce the sets of still-possible answers. – avysk Feb 22 '17 at 17:19

1 Answers1

0

Create a POJO. A Boolean still works because you can have null as an option. When a is selected, a is true. When b is selected, b is false. No selection is null.

class Option {
    Boolean a;
    Boolean c;
    Boolean e;
    Boolean g;
    String value;

    //do some getters and setters

    boolean isValid(Boolean a, Boolean c, Boolean e, Boolean g) {
        if(this.a != a && a != null) {
            return false;
        }
        if(this.c != c && c != null) {
            return false;
        }
        if(this.e != e && e != null) {
            return false;
        }
        if(this.g != g && g != null) {
            return false;
        }

        return true;
    }
}

We can populate our Options using this https://stackoverflow.com/a/27008250/2958086

List<Option> option = new ArrayList<>();
final int n = 4;
for (int i = 0; i < Math.pow(2, n); i++) {
    String bin = Integer.toBinaryString(i);
    while (bin.length() < n)
        bin = "0" + bin;
    char[] chars = bin.toCharArray();
    boolean[] boolArray = new boolean[n];
    for (int j = 0; j < chars.length; j++) {
        boolArray[j] = chars[j] == '0' ? true : false;
    }
    list.add(new Option(boolArray, i+1)); //create this constructor
}

Then, to search:

List<String> findValues(Boolean a, Boolean c, Boolean e, Boolean g) {
    List<String> values = new ArrayList<>();

    for(Option option : options) {
        if(option.isValid(a, c, e, g))
            values.add(option.getValue());
    }
    return values;      
}

To use:

List<String> myValues = findValues(true, false, null, null); // selects all filings that are AD**

Any values that are filled with null will pass, any values that aren't will be compared to their respective values.

Community
  • 1
  • 1
Compass
  • 5,867
  • 4
  • 30
  • 42