Assessment 4_13_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 that the user provides a two one dimensional lists. Write code that returns a new list that is all the numbers in the first list that do not appear in the second list. For example, if the user entered the lists [1, 4, 3, 5, 6, 7, 2]
and [1, 5, 8, 0]
your code would return the list [4, 3, 6, 7, 2]
.
Please do not use any Java code or libraries that we have not covered in class; this defeats the purpose of the assessment and will not receive credit.
Code Template and Test cases
import java.util.Arrays;
public class Assess4_13_Sample{
public static int[] keep(int[] arr1, int[] arr2){
int [] result = new int[2];
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 [] arr1b = {0};
int [] result1 = {1};
System.out.println("test1: " + Arrays.equals(cleanResult(keep(arr1a, arr1b)), result1));
int [] arr2a = {1};
int [] arr2b = {1};
int [] result2 = {};
System.out.println("test2: " + Arrays.equals(cleanResult(keep(arr2a, arr2b)), result2));
int [] arr3a = {};
int [] arr3b = {1};
int [] result3 = {};
System.out.println("test3: " + Arrays.equals(cleanResult(keep(arr3a, arr3b)), result3));
int [] arr4a = {1};
int [] arr4b = {};
int [] result4 = {1};
System.out.println("test4: " + Arrays.equals(cleanResult(keep(arr4a, arr4b)), result4));
int [] arr5a = {1, 2, 1, 4, 5, 3, 7};
int [] arr5b = {1, 3};
int [] result5 = {2, 4, 5, 7};
System.out.println("test5: " + Arrays.equals(cleanResult(keep(arr5a, arr5b)), result5));
}
}