Advertisement
Guest User

Untitled

a guest
Sep 25th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6. public class FizzBuzz {
  7.     static int n = 2;
  8.  
  9.     static class FizzBuzzPair {
  10.         int number;
  11.         Object value; // either an integer between 1 and n, or "fizz", "buzz", or "fizzbuzz"
  12.  
  13.         public FizzBuzzPair(int number, Object value) {
  14.             super();
  15.             this.number = number;
  16.             this.value = value;
  17.         }
  18.  
  19.         public boolean isCorrect() {
  20.             if (number % 15 == 0)
  21.                 return value.equals("fizzbuzz");
  22.             else if (number % 3 == 0)
  23.                 return value.equals("fizz");
  24.             else if (number % 5 == 0)
  25.                 return value.equals("buzz");
  26.             else
  27.                 return (value instanceof Integer) && (number == ((Integer) value).intValue());
  28.         }
  29.     }
  30.  
  31.     public static List<List<FizzBuzzPair>> getAllCombinations() {
  32.         long numCombinations = (long) Math.pow(n + 3, n);
  33.         List<List<FizzBuzzPair>> allCombinations = new ArrayList<>();
  34.         for (long i = 0; i < numCombinations; i++) {
  35.             List<FizzBuzzPair> entry = new ArrayList<>();
  36.             for (int j = 0; j < n; j++) {
  37.                 int val = (int) ((i % (long) Math.pow(n + 3, j + 1)) / ((long) Math.pow(n + 3, j)));
  38.                 Object value;
  39.                 if (val == 0)
  40.                     value = "fizzbuzz";
  41.                 else if (val == n + 1)
  42.                     value = "fizz";
  43.                 else if (val == n + 2)
  44.                     value = "buzz";
  45.                 else
  46.                     value = val;
  47.                 entry.add(new FizzBuzzPair(j + 1, value));
  48.             }
  49.             allCombinations.add(entry);
  50.         }
  51.         return allCombinations;
  52.     }
  53.  
  54.     public static <T> List<List<T>> getAllPermutations(List<T> list, long listLength) {
  55.         List<List<T>> allPermutations = new ArrayList<>();
  56.         if (listLength == 1) {
  57.             allPermutations.add(new ArrayList<>(list));
  58.             return allPermutations;
  59.         }
  60.         for (long i = 0; i < listLength; i++) {
  61.             List<T> copy = new ArrayList<>(list);
  62.             Iterator<T> iter = copy.iterator();
  63.             for (long j = 0; j < i; j++) {
  64.                 iter.next();
  65.             }
  66.             T element = iter.next();
  67.             iter.remove();
  68.             for (List<T> l : getAllPermutations(copy, listLength - 1)) {
  69.                 List<T> copy2 = new ArrayList<>(l);
  70.                 copy2.add(0, element);
  71.                 allPermutations.add(copy2);
  72.             }
  73.         }
  74.         return allPermutations;
  75.     }
  76.  
  77.     public static int numIncorrect(List<FizzBuzzPair> list) {
  78.         int count = 0;
  79.         for (FizzBuzzPair pair : list) {
  80.             if (!(pair.isCorrect()))
  81.                 count++;
  82.         }
  83.         return count;
  84.     }
  85.  
  86.     public static <T> List<T> slowSort(List<T> list, long listLength, Comparator<T> comp) {
  87.         OUTER: for (List<T> permutation : getAllPermutations(list, listLength)) {
  88.             for (long i = 0; i < listLength - 1; i++) {
  89.                 Iterator<T> iter = permutation.iterator();
  90.                 for (long j = 0; j < i; j++) {
  91.                     iter.next();
  92.                 }
  93.                 T element1 = iter.next();
  94.                 T element2 = iter.next();
  95.                 if (comp.compare(element1, element2) > 0) {
  96.                     continue OUTER;
  97.                 }
  98.             }
  99.             return permutation;
  100.         }
  101.         throw new RuntimeException("This line should be unreachable");
  102.     }
  103.  
  104.     public static void main(String[] args) {
  105.         long start = System.currentTimeMillis();
  106.  
  107.         /*
  108.          * FizzBuzz associates every integer from 1 to n with a value; each value is either an
  109.          * integer between 1 and n or "fizz", "buzz", and "fizzbuzz".
  110.          *
  111.          * The allCombinations() method constructs a list of all possible ways to assign one of
  112.          * those n+3 values to each of the first n integers; in total, there are (n+3)^n
  113.          * possibilities.
  114.          */
  115.         List<List<FizzBuzzPair>> allCombinations = getAllCombinations();
  116.  
  117.         /*
  118.          * The list of combinations are then sorted by the number of numbers each combination
  119.          * associates with the incorrect value.
  120.          *
  121.          * The slowSort() method operates by constructing every possible permutation of the input
  122.          * list and then iterating through the permutations until it finds a sorted permutation.
  123.          *
  124.          * Since there are (n+3)^n elements in the list of combinations, slowSort() requires
  125.          * constructing ((n+3)^n)! permutations.
  126.          */
  127.         List<List<FizzBuzzPair>> allCombinationsSorted = slowSort(allCombinations,
  128.                 (long) Math.pow(n + 3, n), Comparator.comparingInt(l -> numIncorrect(l)));
  129.         for (FizzBuzzPair pair : allCombinationsSorted.get(0)) {
  130.             System.out.println(pair.value);
  131.         }
  132.         long stop = System.currentTimeMillis();
  133.         System.out.println();
  134.         System.out.println("Elapsed time: " + ((stop - start) / 1000.0) + " seconds");
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement