Please make sure the test you submit is at least compiling
public class MaxConsecutiveSum{ public static int[] maxConsecutiveSum(int num){ return new int[0]; } }
public class maxConsecutiveSum{- public class MaxConsecutiveSum{
- public static int[] maxConsecutiveSum(int num){
//code goes herereturn [];- return new int[0];
- }
- }
import org.junit.Test; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testSomething() { assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(45), new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}); assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(3), new int[]{1, 2}); assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(2), new int[0]); assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(182), new int[]{44, 45, 46, 47}); } }
- import org.junit.Test;
import static org.junit.Assert.assertEquals;- import static org.junit.Assert.assertArrayEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
- public void testSomething() {
assertEquals(maxConsecutiveSum(45), [1,2,3,4,5,6,7,8,9]);assertEquals(maxConsecutiveSum(3), [1,2]);assertEquals(maxConsecutiveSum(2), []);assertEquals(maxConsecutiveSum(182), [44,45,46,47]);- assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(45),
- new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9});
- assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(3),
- new int[]{1, 2});
- assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(2),
- new int[0]);
- assertArrayEquals(MaxConsecutiveSum.maxConsecutiveSum(182),
- new int[]{44, 45, 46, 47});
- }
- }
It's a bot of a personal thing but I think this makes it easier to read and
understand what is going on at each step.
The tests were all using the same output and input from the first test.
interface HighLow { static int[] findLargestAndSmallest(int[] nums) { if (isCollectionEmpty(nums)) return null; else if (hasOneElement(nums)) return new int[]{nums[0], nums[0]}; else if (hasTwoElements(nums)) return sortTwoElements(nums); return findLargestAndSmallestGeneric(nums); } private static int[] findLargestAndSmallestGeneric(int[] nums) { int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int num : nums) { if (max < num) max = num; else if (min > num) min = num; } return new int[]{min, max}; } static boolean hasOneElement(int[] nums) { return nums.length == 1; } static boolean hasTwoElements(int[] nums) { return nums.length == 2; } private static int[] sortTwoElements(int[] nums) { return (nums[0] <= nums[1]) ? new int[]{nums[0], nums[1]} : new int[]{nums[1], nums[0]}; } private static boolean isCollectionEmpty(int[] nums) { return nums == null || nums.length == 0; } }
import java.util.*;- interface HighLow {
static int[] printLargestAndSmallest(int[] nums) {if(nums == null || nums.length == 0)return null;else if(nums.length == 1)return new int[]{nums[0], nums[0]};else if(nums.length == 2)return (nums[0] <= nums[1]) ? new int[]{nums[0], nums[1]} : new int[]{nums[1], nums[0]};int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;for(int i=0;i<nums.length; i++){if(max<nums[i])max = nums [i];else if(min>nums[i])min=nums[i];}return new int[]{min, max};}- static int[] findLargestAndSmallest(int[] nums) {
- if (isCollectionEmpty(nums))
- return null;
- else if (hasOneElement(nums))
- return new int[]{nums[0], nums[0]};
- else if (hasTwoElements(nums))
- return sortTwoElements(nums);
- return findLargestAndSmallestGeneric(nums);
- }
- private static int[] findLargestAndSmallestGeneric(int[] nums) {
- int max = Integer.MIN_VALUE;
- int min = Integer.MAX_VALUE;
- for (int num : nums) {
- if (max < num)
- max = num;
- else if (min > num)
- min = num;
- }
- return new int[]{min, max};
- }
- static boolean hasOneElement(int[] nums) {
- return nums.length == 1;
- }
- static boolean hasTwoElements(int[] nums) {
- return nums.length == 2;
- }
- private static int[] sortTwoElements(int[] nums) {
- return (nums[0] <= nums[1])
- ? new int[]{nums[0], nums[1]}
- : new int[]{nums[1], nums[0]};
- }
- private static boolean isCollectionEmpty(int[] nums) {
- return nums == null || nums.length == 0;
- }
- }
import org.junit.Test; import static org.junit.Assert.*; import org.junit.runners.JUnit4; // TODO: Replace examples and use TDD by writing your own tests public class SolutionTest { int[] input = new int[]{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1}; int[] output = new int[]{-9,-1}; @Test public void allNegatives() { assertArrayEquals(output, HighLow.findLargestAndSmallest(input)); } int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9}; int[] output2 = new int[]{-9,3}; @Test public void test2() { assertArrayEquals(output2, HighLow.findLargestAndSmallest(input2)); } int[] input3 = new int[]{4}; int[] output3 = new int[]{4,4}; @Test public void oneItem() { assertArrayEquals(output3, HighLow.findLargestAndSmallest(input3)); } int[] input4 = new int[]{9,-1}; int[] output4 = new int[]{-1,9}; @Test public void twoItems() { assertArrayEquals(output4, HighLow.findLargestAndSmallest(input4)); } int[] input5 = new int[]{}; int[] output5 = null; @Test public void emptyArray() { assertArrayEquals(output5, HighLow.findLargestAndSmallest(input5)); } int[] input6 = null; int[] output6 = null; @Test public void nullInput() { assertArrayEquals(output6, HighLow.findLargestAndSmallest(input6)); } }
- import org.junit.Test;
- import static org.junit.Assert.*;
- import org.junit.runners.JUnit4;
- // TODO: Replace examples and use TDD by writing your own tests
- public class SolutionTest {
- int[] input = new int[]{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1};
- int[] output = new int[]{-9,-1};
- @Test
- public void allNegatives() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output, HighLow.findLargestAndSmallest(input));
- }
- int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9};
- int[] output2 = new int[]{-9,3};
- @Test
- public void test2() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output2, HighLow.findLargestAndSmallest(input2));
- }
- int[] input3 = new int[]{4};
- int[] output3 = new int[]{4,4};
- @Test
- public void oneItem() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output3, HighLow.findLargestAndSmallest(input3));
- }
- int[] input4 = new int[]{9,-1};
- int[] output4 = new int[]{-1,9};
- @Test
- public void twoItems() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output4, HighLow.findLargestAndSmallest(input4));
- }
- int[] input5 = new int[]{};
- int[] output5 = null;
- @Test
- public void emptyArray() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output5, HighLow.findLargestAndSmallest(input5));
- }
- int[] input6 = null;
- int[] output6 = null;
- @Test
- public void nullInput() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));- assertArrayEquals(output6, HighLow.findLargestAndSmallest(input6));
- }
- }