Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1.     public static <A> List<List<A>> product(List<A> l) {
  2.         if (l.isEmpty())
  3.             return Collections.singletonList(Collections.emptyList());
  4.         A hd = l.get(0);
  5.         List<List<A>> res = new ArrayList<>();
  6.         List<List<A>> rec = product(l.subList(1, l.size()));
  7.         // Combinations with hd
  8.         for (List<A> r : rec) {
  9.             List<A> hd_r = new ArrayList<>();
  10.             hd_r.add(hd);
  11.             hd_r.addAll(r);
  12.             res.add(hd_r);
  13.         }
  14.         // Combinations without hd
  15.         res.addAll(rec);
  16.         return res;
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         List<Integer> set = new ArrayList<>();
  21.         set.add(0);
  22.         set.add(1);
  23.         set.add(2);
  24.         set.add(3);
  25.         List<List<Integer>> product = product(set);
  26.         System.out.println(product);
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement