-3

If suppose there are three questions and there are multiple checkbox for each question, how do I select all possibilities of checkboxes for these three questions?

So far I got all possible combinations for each question seperately.

For first question there are three answers. The possibilities are:

[A1]
[A2]
[A3]
[A1, A2]
[A1, A3]
[A2, A3]
[A1, A2, A3]

For second question there are two answers. The possibilities are:

[B1]
[B2]
[B1, B2]

So far I got these two lists. I can loop through each element of list using nested for loop to loop through each question:

for (int i = 0; i < questionAList.size(); i++) {
    for (int j = 0; j < questionBList.size(); j++) {
        // select questionA answer
        // for each questionA answer select question B answer
    }
}

This way I can select all the possible answers for multiple questions.

But this nested for loop works only if there are only two questions. How can I solve this with a more generic approach?

Here is what I have got so far:

{
  "Q: Headache:" : {
    "1" : "[wakes you up at night]",
    "2" : "[about the same time of day]",
    "3" : "[None of the above]",
    "4" : "[wakes you up at night, about the same time of day]"
  },
    "Q: Confusion" : {
    "1" : "[better with drinking fluids]",
    "2" : "[better with rest]",
    "3" : "[None of the above]",
    "4" : "[better with drinking fluids, better with rest]"
  },
   "Q: Confusion associated with …" : {
    "1" : "[HIV illness]",
    "2" : "[None of the above]"
  }
}

There is only combination for answer "None of the above".

Abhilash
  • 435
  • 5
  • 15
  • what is this? a web application? swing? – Scary Wombat Jul 19 '17 at 04:42
  • I am trying to scrape data on a website using Selenium, PhantomJS & Jsoup. – Abhilash Jul 19 '17 at 04:44
  • 1
    You omitted one possibility: no boxes checked. Hint: unchecked=0 checked=1; number of possibilities for n checkboxes is 2*n – Jim Garrison Jul 19 '17 at 04:44
  • Yes. The last answer for each question is "None of the above". So I excluded all the combinations which has "None of the above" and included only one such combination. – Abhilash Jul 19 '17 at 04:51
  • That's where your model breaks down and over-complicates the picture. Reduce it to a simple binary string with 1 for checked and 0 for unchecked. If the user interface needs a "none of the above" then map it internally to a model that represents "none of the above" with all zeros. – Jim Garrison Jul 19 '17 at 04:52

1 Answers1

0
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.google.common.collect.Sets;

public class CombinationsTest {

    public static void main(String[] args) {

        Map<Integer, Integer> questionAnswersMap = new LinkedHashMap<>();
        questionAnswersMap.put(1, 2);
        questionAnswersMap.put(2, 3);

        Set<Set<Integer>> combinations = new LinkedHashSet<>();
        List<Set<Set<Integer>>> combinationsList = new LinkedList<>();

        for (Entry<Integer, Integer> entry : questionAnswersMap.entrySet()) {
            combinations = getCombinations(createHashSet(entry.getValue()));
            combinationsList.add(combinations);
        }

        for(Set set: combinationsList){
            System.out.println(set);
        }
        Set<List<Set<Integer>>> totalCombinations = Sets.cartesianProduct(combinationsList);

        List<Set<Integer>> result = new LinkedList<>();
        process(totalCombinations, result);

        for (int i = 1; i <= result.size(); i++) {
            System.out.println("Combination " + i + " of " + result.size() + " : " + result.get(i-1));
        }

    }

    public static void process(Set<List<Set<Integer>>> totalCombinations, List<Set<Integer>> result) {

        for (List<Set<Integer>> combinations : totalCombinations) {
            result.addAll(combinations);
        }
    }

    public static Set<Set<Integer>> getCombinations(Set<Integer> options) {

        Set<Set<Integer>> combinations = new LinkedHashSet<>();

        for (int i = 1; i <= options.size(); i++) {
            Set<Set<Integer>> temp = generateCombinations(options, i);
            combinations.addAll(temp);

            for (Set<Integer> set : temp) {
                if (set.size() > 1 && set.contains(options.size())) {
                    combinations.remove(set);
                }
            }
        }

        return combinations;
    }

    protected static Set<Set<Integer>> generateCombinations(Set<Integer> options, int size) {

        Set<Set<Integer>> elements = Sets.powerSet(options);
        Set<Set<Integer>> possibleCombinations = elements.stream().filter(p -> p.size() == size)
                .collect(Collectors.toSet());

        return possibleCombinations;
    }

    public static Set<Integer> createHashSet(int answersCount) {

        Set<Integer> answers = Sets.newHashSet();
        for (int i = 1; i <= answersCount; i++) {
            answers.add(i);
        }
        return answers;
    }
}

output:

[[1], [2]]
[[1], [2], [3], [1, 2]] 
Combination 1 of 16 : [1]
Combination 2 of 16 : [1]
Combination 3 of 16 : [1]
Combination 4 of 16 : [2]
Combination 5 of 16 : [1]
Combination 6 of 16 : [3]
Combination 7 of 16 : [1] 
Combination 8 of 16 : [1, 2]
Combination 9 of 16 : [2]
Combination 10 of 16 : [1]
Combination 11 of 16 : [2]
Combination 12 of 16 : [2]
Combination 13 of 16 : [2]
Combination 14 of 16 : [3]
Combination 15 of 16 : [2]
Combination 16 of 16 : [1, 2]
Abhilash
  • 435
  • 5
  • 15