2017-07-19 9 views
-3

3つの質問があり、それぞれの質問に対して複数のチェックボックスがある場合、これらの3つの質問のチェックボックスの可能性をすべて選択するにはどうすればよいですか?複数の質問に対して可能なすべてのチェックボックスの組み合わせを選択するにはどうすればよいですか?

これまでのところ、それぞれの質問ごとにすべての組み合わせを別々に得ました。

最初の質問には3つの回答があります。可能性は次のとおりです。

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

2番目の質問には2つの回答があります。可能性は以下の通りです:

[B1] 
[B2] 
[B1, B2] 

これまで私はこれらの2つのリストを得ました。ループのネストされたforループを使用して各質問をループすることができます。

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 
    } 
} 

このようにして、複数の質問に対してすべての回答を選択することができます。

しかし、このネストされたforループは、質問が2つしかない場合にのみ機能します。もっと一般的なアプローチでこれをどのように解決できますか?

{ 
    "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]" 
    } 
} 

答え「上のなし」のための唯一の組み合わせがあります:ここで

は、私がこれまで持っているものです。

+0

これは何ですか? Webアプリケーションですか?スイング? –

+0

私はSelenium、PhantomJS&Jsoupを使ってウェブサイト上のデータを削り取ろうとしています。 – Abhilash

+1

あなたは1つの可能性を省いた:ボックスはチェックされていない。ヒント:未チェック= 0チェック= 1; n個のチェックボックスの可能性は2 * n –

答えて

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; 
    } 
} 

出力:

[[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] 
+0

最後にコンソール出力を追加しました。 – Abhilash

関連する問題