Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. public class Fermat {
  2.  
  3.     public static Set<List<String>> threadings(int n, Set<String> colours) {
  4.         if (n == 0) {
  5.             return Collections.singleton(new ArrayList<>());
  6.         }
  7.  
  8.         Set<List<String>> previousThreadings = threadings(n - 1, colours);
  9.         Set<List<String>> finalResult = new LinkedHashSet<>();
  10.         for (List<String> beadList : previousThreadings) {
  11.             for (String colour : colours) {
  12.                 List<String> newBeadList = new ArrayList<>(beadList);
  13.                 newBeadList.add(colour);
  14.                 finalResult.add(newBeadList);
  15.             }
  16.         }
  17.  
  18.         return finalResult;
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.         Set<String> colours = new HashSet<>();
  23.         colours.add("red");
  24.         colours.add("green");
  25.         colours.add("blue");
  26.  
  27.         System.out.println(threadings(2, colours));
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement