Assessment 3_3_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. Return a new list where you add the number 3 after every number 3 in the original list. For example, if the original list was [4,5,3,3,6,5,3] the new list would be [4,5,3,3,3,3,6,5,3,3].

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_3_Sample{

  public static int[] addThree(int[] original){
    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 [] arr1 = {};
      int [] result1 = {};
      System.out.println("test1: " + Arrays.equals(cleanResult(addThree(arr1)), result1));
      int [] arr2 = {1};
      int [] result2 = {1};
      System.out.println("test2: " + Arrays.equals(cleanResult(addThree(arr2)), result2));
      int [] arr3 = {1, 3};
      int [] result3 = {1, 3, 3};
      System.out.println("test3: " + Arrays.equals(cleanResult(addThree(arr3)), result3));
      int [] arr4 = {2, 1, 3, 4};
      int [] result4 = {2, 1, 3, 3, 4};
      System.out.println("test4: " + Arrays.equals(cleanResult(addThree(arr4)), result4));
      int [] arr5 = {1, 2, 3, 3};
      int [] result5 = {1, 2, 3, 3, 3, 3};
      System.out.println("test5: " + Arrays.equals(cleanResult(addThree(arr5)), result5));
      int [] arr6 = {4,5,3,3,6,5,3};
      int [] result6 = {4, 5, 3, 3, 3, 3, 6, 5, 3, 3};
      System.out.println("test6: " + Arrays.equals(cleanResult(addThree(arr6)), result6));

  }

}