Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- public class TestCheat20171205Test extends TestCase {
- public static int valueFromFirstLongestSequence(int[] arr) {
- int indexToLargestCountSoFar = 0;
- int largestCountSoFar = 1;
- int count = 1;
- for (int idx = 1; idx < arr.length; ++idx) {
- if (arr[idx] == arr[idx-1]) {
- ++count;
- if (count > largestCountSoFar) {
- largestCountSoFar = count;
- indexToLargestCountSoFar = idx;
- }
- } else {
- count = 1;
- }
- }
- return arr[indexToLargestCountSoFar];
- }
- public static void main(String[] args) {
- 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};
- System.out.println(valueFromFirstLongestSequence(arr));
- }
- public void testOriginalExample() {
- 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}));
- 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}));
- 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}));
- 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}));
- 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}));
- }
- public void testSimpleSequences() {
- assertEquals(2, valueFromFirstLongestSequence(new int[] {1, 2,2, 3}));
- assertEquals(1, valueFromFirstLongestSequence(new int[] {1,1, 2,2, 3,3}));
- assertEquals(3, valueFromFirstLongestSequence(new int[] {1,1, 2,2, 3,3,3}));
- assertEquals(2, valueFromFirstLongestSequence(new int[] {2,2, 1, 3,3}));
- assertEquals(1, valueFromFirstLongestSequence(new int[] {1}));
- assertEquals(2, valueFromFirstLongestSequence(new int[] {1, 2,2}));
- assertEquals(3, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3}));
- assertEquals(4, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3, 4,4,4,4}));
- assertEquals(5, valueFromFirstLongestSequence(new int[] {1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5}));
- }
- public void testNoSequence_ReturnsFirst() {
- assertEquals(6, valueFromFirstLongestSequence(new int[] {6}));
- assertEquals(1, valueFromFirstLongestSequence(new int[] {1,2}));
- assertEquals(3, valueFromFirstLongestSequence(new int[] {3,1,2}));
- }
- public void testNoInput_throws_ArrayIndexOutOfBoundsException() { // Unfortunate, but true. :...(
- try {
- valueFromFirstLongestSequence(new int[] {});
- fail("Expected ArrayIndexOutOfBoundsException.");
- } catch (ArrayIndexOutOfBoundsException ex) {
- assertEquals("0", ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement