Advertisement
JeffGrigg

Untitled

Dec 5th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.91 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. public class TestCheat20171205Test extends TestCase {
  4.  
  5.     public static int valueFromFirstLongestSequence(int[] arr) {
  6.         int indexToLargestCountSoFar = 0;
  7.         int largestCountSoFar = 1;
  8.         int count = 1;
  9.         for (int idx = 1; idx < arr.length; ++idx) {
  10.             if (arr[idx] == arr[idx-1]) {
  11.                 ++count;
  12.                 if (count > largestCountSoFar) {
  13.                     largestCountSoFar = count;
  14.                     indexToLargestCountSoFar = idx;
  15.                 }
  16.             } else {
  17.                 count = 1;
  18.             }
  19.         }
  20.         return arr[indexToLargestCountSoFar];
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         int[] arr = {1,2,3,3,3,4,2,2,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8};
  25.         System.out.println(valueFromFirstLongestSequence(arr));
  26.     }
  27.  
  28.     public void testOriginalExample() {
  29.         assertEquals(2, valueFromFirstLongestSequence(new int[] {1,2,3,3,3,4,2,2,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8}));
  30.  
  31.         assertEquals(2, valueFromFirstLongestSequence(new int[] {1,2,3,3,3,4,2,2,2,2,2,2,2,5,6,6,6,6,6,6,6,4,7,8}));
  32.         assertEquals(6, valueFromFirstLongestSequence(new int[] {1,2,3,3,3,4,2,2,2,2,2,2,2,5,6,6,6,6,6,6,6,6,4,7,8}));
  33.         assertEquals(2, valueFromFirstLongestSequence(new int[] {1,2,3,3,3,4,2,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8}));
  34.         assertEquals(6, valueFromFirstLongestSequence(new int[] {1,2,3,3,3,4,2,2,2,2,2,5,6,6,6,6,6,6,4,7,8}));
  35.     }
  36.  
  37.     public void testSimpleSequences() {
  38.         assertEquals(2, valueFromFirstLongestSequence(new int[] {1, 2,2, 3}));
  39.         assertEquals(1, valueFromFirstLongestSequence(new int[] {1,1, 2,2, 3,3}));
  40.         assertEquals(3, valueFromFirstLongestSequence(new int[] {1,1, 2,2, 3,3,3}));
  41.  
  42.         assertEquals(2, valueFromFirstLongestSequence(new int[] {2,2, 1, 3,3}));
  43.  
  44.         assertEquals(1, valueFromFirstLongestSequence(new int[] {1}));
  45.         assertEquals(2, valueFromFirstLongestSequence(new int[] {1, 2,2}));
  46.         assertEquals(3, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3}));
  47.         assertEquals(4, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3, 4,4,4,4}));
  48.         assertEquals(5, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5}));
  49.     }
  50.  
  51.     public void testNoSequence_ReturnsFirst() {
  52.         assertEquals(6, valueFromFirstLongestSequence(new int[] {6}));
  53.         assertEquals(1, valueFromFirstLongestSequence(new int[] {1,2}));
  54.         assertEquals(3, valueFromFirstLongestSequence(new int[] {3,1,2}));
  55.     }
  56.  
  57.     public void testNoInput_throws_ArrayIndexOutOfBoundsException() {   // Unfortunate, but true.   :...(
  58.         try {
  59.             valueFromFirstLongestSequence(new int[] {});
  60.             fail("Expected ArrayIndexOutOfBoundsException.");
  61.         } catch (ArrayIndexOutOfBoundsException ex) {
  62.             assertEquals("0", ex.getMessage());
  63.         }
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement