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; 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; } }
- 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)- 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[] input123 = new int[]{1,2,3}; int[] output123 = new int[]{1,3}; @Test public void test21() { assertArrayEquals(output123, HighLow.findLargestAndSmallest(input123)); } 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.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[] input123 = new int[]{1,2,3};
- int[] output123 = new int[]{1,3};
- @Test
- public void test21() {
- assertArrayEquals(output123, HighLow.findLargestAndSmallest(input123));
- }
- 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));
- }
- }