Assessment 3_15_sample

Instructions

This assessment is designed to be completed in 10 minutes or less.

Copy the template below and get all the test cases to pass. Hard-coding answers will not receieve credit; your solution must work for valid arrays.

The problem

Imagine the user enters a list of integers. First, split the array into sub-arrays of odd and even numbers, and then glue these two arrays back together and return the result. For example, if the user entered [1, 8, 5, 7, 2, 4, 4, 5], you would return the list [1, 5, 7, 5, 8, 2, 4, 4].

To save time, we’ve created a result array for you that is 100 elements long, and filled with zeroes; you can simply place the kept items into this array, in order. The grading script will remove the extra zeroes at the end automatically (and you can take a look at the cleanResult method in the template if you’re curious how it works).

Code Template and Test cases

import java.util.Arrays;

public class Assess3_15_Sample{

  public static int[] separateOddsEvens(int[] arr){
    int[] result = new int[10];

    return result;
  }

  public static int[] cleanResult(int[] array){
    return Arrays.stream(array).filter(num -> num != 0).toArray();    
  }

  public static void main(String[] args){
      int [] arr1a = {1};
      int [] result1 = {1};
      System.out.println("test1: " + Arrays.equals(cleanResult(separateOddsEvens(arr1a)), result1));
      int [] arr2a = {1, 2};
      int [] result2 = {1, 2};
      System.out.println("test2: " + Arrays.equals(cleanResult(separateOddsEvens(arr2a)), result2));
      int [] arr3a = {2, 1};
      int [] result3 = {1, 2};
      System.out.println("test3: " + Arrays.equals(cleanResult(separateOddsEvens(arr3a)), result3));
      int [] arr4a = {2, 1, 3};
      int [] result4 = {1, 3, 2};
      System.out.println("test4: " + Arrays.equals(cleanResult(separateOddsEvens(arr4a)), result4));
      int [] arr5a = {1, 2, 3, 3};
      int [] result5 = {1, 3, 3, 2};
      System.out.println("test5: " + Arrays.equals(cleanResult(separateOddsEvens(arr5a)), result5));
      int [] arr6a = {7, 1, 2, 3, 2, 4, 1, 3};
      int [] result6 = {7, 1, 3, 1, 3, 2, 2, 4};
      System.out.println("test6: " + Arrays.equals(cleanResult(separateOddsEvens(arr6a)), result6));
      int [] arr7a = {8, 1, 2, 3, 2, 4, 1, 3};
      int [] result7 = {1, 3, 1, 3, 8, 2, 2, 4};
      System.out.println("test7: " + Arrays.equals(cleanResult(separateOddsEvens(arr7a)), result7));
  }

}