Advertisement
teleias

Set of All Sets of List

Nov 6th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. /* The goal of this project is to,
  2.  *  Given a list of string (inputList),
  3.  *   Create a set of sets (masterList)
  4.  *    such that the masterList contains every possible set
  5.  *    that could be created from the input list.
  6.  *  This function is called by creating a list of string
  7.  *   and calling getAllSets, with input as a parameter.
  8.  *  Store the output if you like. If not, this currently prints everything out to console.
  9.  *  Have fun converting this to C#. Should be easy.
  10.  */
  11.  
  12.  
  13.     public static void main(String[] args)
  14.     {
  15.         List<String> input = Arrays.asList("a", "b", "c", "d", "e");
  16.         getAllSets(input);
  17.     }
  18.     public static List<List<String>> getAllSets(List<String> input)
  19.     {
  20.         List<List<String>> masterSet = new ArrayList<List<String>>();
  21.         int size = input.size();
  22.        
  23.         //end at math.pow(2,size)-1, because math.pow(2,size) will always be empty set
  24.         for(int i = 0; i < Math.pow(2, size)-1; i++)
  25.         {
  26.             List<String> slaveSet = new ArrayList<String>();
  27.             for(int j = 0; j < size; j++)
  28.             {
  29.                 int x = (int)Math.pow(2, j+1);
  30.                 if(i%x < x/2)
  31.                     slaveSet.add(input.get(j));
  32.             }
  33.             masterSet.add(slaveSet);
  34.             System.out.println("\t"+slaveSet);
  35.         }
  36.         return masterSet;
  37.     }
  38.  
  39.  
  40.  
  41. /*      SAMPLE OUTPUT
  42.     [a, b, c, d, e]
  43.     [b, c, d, e]
  44.     [a, c, d, e]
  45.     [c, d, e]
  46.     [a, b, d, e]
  47.     [b, d, e]
  48.     [a, d, e]
  49.     [d, e]
  50.     [a, b, c, e]
  51.     [b, c, e]
  52.     [a, c, e]
  53.     [c, e]
  54.     [a, b, e]
  55.     [b, e]
  56.     [a, e]
  57.     [e]
  58.     [a, b, c, d]
  59.     [b, c, d]
  60.     [a, c, d]
  61.     [c, d]
  62.     [a, b, d]
  63.     [b, d]
  64.     [a, d]
  65.     [d]
  66.     [a, b, c]
  67.     [b, c]
  68.     [a, c]
  69.     [c]
  70.     [a, b]
  71.     [b]
  72.     [a]
  73. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement