Advertisement
JeffGrigg

ShiftAllZerosToRightTest

Jun 12th, 2018
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 7.80 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.Stream;
  7.  
  8. public class ShiftAllZerosToRightTest extends TestCase {
  9.  
  10.     private static int[] shiftZerosToRight(final int[] inputArray) {
  11.  
  12.         // Go "right to left", pushing non-zero values onto the stack, and setting all array values to zero:
  13.         final IntStack stack = new IntStack(inputArray.length);
  14.         for (int idx = inputArray.length - 1; idx >= 0; --idx) {
  15.             final int value = inputArray[idx];
  16.             if (value != 0) {
  17.                 stack.push(value);
  18.             }
  19.             inputArray[idx] = 0;
  20.         }
  21.  
  22.         // "Left to right" to "lay the valued down from the stack"  (... preserving the order, as we're popping in the reverse direction from when we pushed.)
  23.         int idx = 0;
  24.         while (stack.hasElements()) {
  25.             inputArray[idx++] = stack.pop();
  26.         }
  27.  
  28.         // Return the array, "modified in place."
  29.         return inputArray;
  30.     }
  31.  
  32.     /**
  33.      * Simplistic specialized Stack, as I don't like the synchronized methods of the Java Stack class, nor its lack of
  34.      * initialization to a given size.
  35.      */
  36.     private static class IntStack {
  37.         int _idx = 0;
  38.         final int[] _values;
  39.  
  40.         public IntStack(final int initialSize) {
  41.             _values = new int[initialSize];
  42.         }
  43.  
  44.         public void push(final int value) {
  45.             _values[_idx++] = value;
  46.         }
  47.  
  48.         public boolean hasElements() {
  49.             return _idx > 0;
  50.         }
  51.  
  52.         public int pop() {
  53.             return _values[--_idx];
  54.         }
  55.     }
  56.  
  57.     private static int[] mostEfficient(final int[] inputArray) {
  58.         int position = 0;
  59.         for (int i = 0; i < inputArray.length; i++) {
  60.             if (inputArray[i] != 0) {
  61.                 inputArray[position] = inputArray[i];
  62.                 position++;
  63.             }
  64.         }
  65.  
  66.         /*Assign zero to remaining elements*/
  67.         while (position < inputArray.length) {
  68.             inputArray[position] = 0;
  69.  
  70.             position++;
  71.         }
  72.  
  73.         return inputArray;
  74.     }
  75.  
  76.     private static int[] arrayCopyNonZeros(final int[] inputArray) {
  77.         final int[] copy = new int[inputArray.length];
  78.         // Copy non-zero values to the new array:
  79.         int idx = 0;
  80.         for (int value : inputArray) {
  81.             if (value != 0) {
  82.                 copy[idx++] = value;
  83.             }
  84.         }
  85.         // (Remaining cells in "copy" array were initialized by Java to be zero.
  86.  
  87.         // Copy the result back:
  88.         idx = 0;
  89.         for (int value : copy) {
  90.             inputArray[idx++] = value;
  91.         }
  92.  
  93.         return inputArray;
  94.     }
  95.  
  96.     private static int[] quickCopy(final int[] inputArray) {
  97.         for (int leftIdx = 0, rightIdx = 1; rightIdx < inputArray.length; rightIdx++) {
  98.             if (inputArray[leftIdx] == 0) {
  99.                 if (inputArray[rightIdx] != 0) {
  100.                     inputArray[leftIdx] = inputArray[rightIdx];
  101.                     inputArray[rightIdx] = 0;
  102.                     leftIdx++;
  103.                 }
  104.             } else {
  105.                 leftIdx++;
  106.             }
  107.         }
  108.         return inputArray;
  109.     }
  110.  
  111.     private static Integer[] withStreams(final Integer[] nums) {
  112.         List<Integer> nonzeros = Arrays.stream(nums).filter(num -> num != 0).collect(Collectors.toList());
  113.         List<Integer> zeros = Arrays.stream(nums).filter(num -> num == 0).collect(Collectors.toList());
  114.         List<Integer> result = Stream.concat(nonzeros.stream(), zeros.stream()).collect(Collectors.toList());
  115.         return result.toArray(new Integer[nums.length]);
  116.     }
  117.  
  118.  
  119.     public void testEmpty() {
  120.         assertEqualsAfterShifting(new int[]{}, new int[]{});
  121.     }
  122.  
  123.     public void testNoZeros() {
  124.         assertEqualsAfterShifting(new int[]{1}, new int[]{1});
  125.         assertEqualsAfterShifting(new int[]{1, 2}, new int[]{1, 2});
  126.         assertEqualsAfterShifting(new int[]{1, 2, 3}, new int[]{1, 2, 3});
  127.     }
  128.  
  129.     public void testZerosAtEnd() {
  130.         assertEqualsAfterShifting(new int[]{1, 0}, new int[]{1, 0});
  131.         assertEqualsAfterShifting(new int[]{1, 0, 0}, new int[]{1, 0, 0});
  132.         assertEqualsAfterShifting(new int[]{1, 2, 0}, new int[]{1, 2, 0});
  133.     }
  134.  
  135.     public void testOneZeroToMove() {
  136.         assertEqualsAfterShifting(new int[]{1, 0}, new int[]{0, 1});
  137.         assertEqualsAfterShifting(new int[]{1, 2, 0}, new int[]{1, 0, 2});
  138.         assertEqualsAfterShifting(new int[]{1, 2, 0}, new int[]{0, 1, 2});
  139.     }
  140.  
  141.     public void testOneDigitTwoZeros() {
  142.         assertEqualsAfterShifting(new int[]{1, 0, 0}, new int[]{0, 1, 0});
  143.         assertEqualsAfterShifting(new int[]{1, 0, 0}, new int[]{0, 0, 1});
  144.     }
  145.  
  146.     public void testAllZeros() {
  147.         assertEqualsAfterShifting(new int[]{0}, new int[]{0});
  148.         assertEqualsAfterShifting(new int[]{0, 0}, new int[]{0, 0});
  149.         assertEqualsAfterShifting(new int[]{0, 0, 0}, new int[]{0, 0, 0});
  150.         assertEqualsAfterShifting(new int[]{0, 0, 0, 0}, new int[]{0, 0, 0, 0});
  151.     }
  152.  
  153.     public void testNegativeValues() {
  154.         assertEqualsAfterShifting(new int[]{-1, -2, 0, 0, 0}, new int[]{0, -1, 0, -2, 0});
  155.     }
  156.  
  157.     public void testMultipleZerosAndNumbers() {
  158.         assertEqualsAfterShifting(new int[]{1, 2, 3, 4, 0, 0, 0, 0, 0, 0}, new int[]{0, 0, 1, 2, 0, 0, 3, 4, 0, 0});
  159.     }
  160.  
  161.     public void testMinAndMaxIntegerValues() {
  162.         assertEqualsAfterShifting(new int[]{1, Integer.MAX_VALUE, -1, 11, Integer.MIN_VALUE, -45, 0}, new int[]{1, Integer.MAX_VALUE, -1, 0, 11, Integer.MIN_VALUE, -45});
  163.     }
  164.  
  165.     public void testOtherValues() {
  166.         assertEqualsAfterShifting(new int[]{-3, -2, -1, 1, 2, 3, 0}, new int[]{-3, -2, -1, 0, 1, 2, 3});
  167.     }
  168.  
  169.     public void testAssertArraysAreEquals() {
  170.         try {
  171.             assertEquals("YourMessageHere;", new int[]{1, 2, 3}, new int[]{3, 2, 1});
  172.             throw new IllegalArgumentException("Expected AssertionError.");
  173.         } catch (final AssertionError ex) {
  174.             assertEquals("YourMessageHere; Arrays are not equal: expected <[1, 2, 3]> but was <[3, 2, 1]>", ex.getMessage());
  175.         }
  176.     }
  177.  
  178.     private static void assertEqualsAfterShifting(final int[] expectedValues, final int[] inputValues) {
  179.         assertEquals("shiftZerosToRight;", expectedValues, shiftZerosToRight(Arrays.copyOf(inputValues, inputValues.length)));
  180.         assertEquals("mostEfficient;", expectedValues, mostEfficient(Arrays.copyOf(inputValues, inputValues.length)));
  181.         assertEquals("arrayCopyNonZeros;", expectedValues, arrayCopyNonZeros(Arrays.copyOf(inputValues, inputValues.length)));
  182.         assertEquals("quickCopy;", expectedValues, quickCopy(Arrays.copyOf(inputValues, inputValues.length)));
  183.  
  184.         assertEquals("withStreams;", expectedValues, toIntArray(withStreams(toIntegerArray(inputValues))));
  185.     }
  186.  
  187.     private static Integer[] toIntegerArray(final int[] inputValues) {
  188.         final Integer[] result = new Integer[inputValues.length];
  189.         int idx = 0;
  190.         for (int value : inputValues) {
  191.             result[idx++] = value;
  192.         }
  193.         return result;
  194.     }
  195.  
  196.     private static int[] toIntArray(final Integer[] inputValues) {
  197.         final int[] result = new int[inputValues.length];
  198.         int idx = 0;
  199.         for (int value : inputValues) {
  200.             result[idx++] = value;
  201.         }
  202.         return result;
  203.     }
  204.  
  205.     private static void assertEquals(final String message, final int[] expectedValues, final int[] actualValues) {
  206.         if (!Arrays.equals(expectedValues, actualValues)) {
  207.             throw new AssertionError(message + " Arrays are not equal: expected <" + Arrays.toString(expectedValues) + "> but was <" + Arrays.toString(actualValues) + ">");
  208.         }
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement