Page 1 of 1

public class Question9 { //begin class /** * @param list * @return a list containing the longest repeating subl

Posted: Fri Jun 10, 2022 11:58 am
by correctanswer
public class Question9 { //begin class
/**
* @param list
* @return a list containing the longest repeating
sublist inside the parameter.
* return first of the sublists if there is a
tie.
*
*/
public static ArrayList<Integer>
getLongestRepeatingSubList(ArrayList<Integer> list) {
return null;
}
private static ArrayList<Integer>
get(ArrayList<Integer> list, int start, int n) {
if(list == null || list.size()
<= start + n)
return
null;
ArrayList<Integer> result =
new ArrayList<Integer>();
for(int i=start; i < start + n;
i++) {

result.add(list.get(i));
}
return result;
}
private static int
lastIndexOf(ArrayList<Integer> list, ArrayList<Integer>
sub) {
if(list == null || sub == null ||
list.size() < sub.size())
return -1;
for(int i=list.size()-sub.size(); i
>= 0; i--) {

if(list.subList(i, i+sub.size()).equals(sub))

return i;
}
return -1;
}
@Test
public void testGetLongestRepeatingSubList() throws
NoSuchMethodException, SecurityException {
ArrayList<Integer> list =
null;

assertNull(Question9.getLongestRepeatingSubList(list));
list = new
ArrayList<Integer>();

assertNull(Question9.getLongestRepeatingSubList(list));
list.add(10);

assertNull(Question9.getLongestRepeatingSubList(list));
list.add(10);

assertNotNull(Question9.getLongestRepeatingSubList(list));
assertEquals("[10]",
Question9.getLongestRepeatingSubList(list).toString());
list.add(10);

assertNotNull(Question9.getLongestRepeatingSubList(list));
assertEquals("[10, 10]",
Question9.getLongestRepeatingSubList(list).toString());
list.add(2, 20);
list.add(10);
//10,10,20,10,10

assertNotNull(Question9.getLongestRepeatingSubList(list));
assertEquals("[10, 10]",
Question9.getLongestRepeatingSubList(list).toString());
list = new
ArrayList<Integer>(Arrays.asList(10,70,20,90,30,80,20,90,30,10,70,20));


assertNotNull(Question9.getLongestRepeatingSubList(list));
assertEquals("[10, 70, 20]",
Question9.getLongestRepeatingSubList(list).toString());
list = new
ArrayList<Integer>(Arrays.asList(2,2,2,2,2,2,2,2,2,2,2,2));


assertNotNull(Question9.getLongestRepeatingSubList(list));
assertEquals("[2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2]",
Question9.getLongestRepeatingSubList(list).toString());


}
}
public class Question10 { //begin class
/**
*
* @param start: starting index
* @param nums: array
* @param target
* @return true if it is possible (and false if
not)
* to choose a group of some of the ints (starting at
index start),
* such that the groups product is the
* given target with these additional
constraints:
* all multiples of 5 in the array must be
included
* in the group. If the value immediately
following
* a multiple of 5 is 1, the 1 must not be
chosen.
* (Solution must be recursive AND should not contain
any loop)
*/
public static boolean productRecursive(int start,
int[] nums, int target) {
return false;
}
@Test
public void testProductRecursive() throws NoSuchMethodException,
SecurityException {
int[] a = {2,5,10,4};

assertTrue(Question10.productRecursive(0, a, 50));

assertFalse(Question10.productRecursive(0, a, 60));
int[] b = {3,5,2};

assertFalse(Question10.productRecursive(0, b, 6));

assertTrue(Question10.productRecursive(0, b, 30));

assertFalse(Question10.productRecursive(0, b, 8));
int[] c =
{1,2,3,4,5,6,7,8,9,10};

assertTrue(Question10.productRecursive(0, c, 3628800));

assertFalse(Question10.productRecursive(2, c, 3628800));
}
}
public class Question11 { //begin class
/**
*
* @param list: list of items
* @param start: starting index
* @param target: value to construct
* @return list containing **indices** (from lower to
higher) of items that add up to the given target.
* all combinations with a given item must be
considered before rejecting it.
* return false if there is no combination of items
adding up to target
* if list = [30,80,-20,200,10], start = 0, target =
10, return list [0, 2] as list.get(0) + list.get(2) = 10
* if list = [30,80,-20,200,10], start = 0, target =
50, return null
*/
public static ArrayList<Integer>
indicesAddingUpto(ArrayList<Integer> list, int start, int
target) {
return null;
}
public void testIndicesAddingUpto() throws
NoSuchMethodException, SecurityException {
ArrayList<Integer> a = new
ArrayList<Integer>(Arrays.asList(2,5,10,4));

assertNotNull(Question11.indicesAddingUpto(a, 0, 11));
assertEquals("[0, 1, 3]",
Question11.indicesAddingUpto(a, 0, 11).toString());

assertNull(Question11.indicesAddingUpto(a, 0, 8));
ArrayList<Integer> b = new
ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));


assertNotNull(Question11.indicesAddingUpto(b, 0, 50));
assertEquals("[0, 1, 2, 3, 5, 6, 7,
8, 9]", Question11.indicesAddingUpto(b, 0, 50).toString());

assertNull(Question11.indicesAddingUpto(b, 0, 56));
ArrayList<Integer> c = new
ArrayList<Integer>(Arrays.asList(1, 10, 2, 30, 3, 50, 4, 70,
5, 90, 6, 100));

assertNotNull(Question11.indicesAddingUpto(c, 0, 21));
assertEquals("[0, 1, 2, 4, 8]",
Question11.indicesAddingUpto(c, 0, 21).toString());
ArrayList<Integer> empty =
new ArrayList<Integer>();

assertNotNull(Question11.indicesAddingUpto(empty, 0, 0));
assertEquals("[]",
Question11.indicesAddingUpto(empty, 0, 0).toString());

assertNull(Question11.indicesAddingUpto(empty, 0, 1));
}
}