Assessment 3_14_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 two lists of integers, where the second is always less than or equal to three elements long. Write code that will return true
or false
whether or not the shorter list is a sub-list of the first list. For example, if the user enters the list [7, 1, 2, 3, 2, 4, 1, 3]
and [3, 2, 4]
your code would return true
, but if the second list was [3, 2, 3]
your code would return false
.
Code Template and Test cases
public class Assess3_14_Sample{
public static boolean subList(int[] longer, int[] sub){
return false;
}
public static void main(String[] args){
int [] arr1a = {1};
int [] arr1b = {};
System.out.println("test1: " + (subList(arr1a, arr1b) == true));
int [] arr2a = {1, 2};
int [] arr2b = {1};
System.out.println("test2: " + (subList(arr2a, arr2b) == true));
int [] arr3a = {1, 3};
int [] arr3b = {3};
System.out.println("test3: " + (subList(arr3a, arr3b) == true));
int [] arr4a = {2, 1, 3};
int [] arr4b = {1, 3};
System.out.println("test4: " + (subList(arr4a, arr4b) == true));
int [] arr5a = {1, 2, 3, 3};
int [] arr5b = {1, 2};
System.out.println("test5: " + (subList(arr5a, arr5b) == true));
int [] arr6a = {7, 1, 2, 3, 2, 4, 1, 3};
int [] arr6b = {3, 2, 4};
System.out.println("test6: " + (subList(arr6a, arr6b) == true));
int [] arr7a = {7, 1, 2, 3, 2, 4, 1, 3};
int [] arr7b = {3, 2, 3};
System.out.println("test7: " + (subList(arr7a, arr7b) == false));
}
}