-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.java
More file actions
67 lines (60 loc) · 2.14 KB
/
Q2.java
File metadata and controls
67 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
*
*/
package a1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class Q2 {
// Return all sums that can be formed from subsets of elements in arr
public static ArrayList<Integer> allSums(ArrayList<Integer> arr) {
ArrayList<Integer> arrayListSum = new ArrayList<Integer>();
int num = arr.size() - 1;
// if arr isn't empty
if (!arr.isEmpty()) {
ArrayList<Integer> arrSublist = new ArrayList<Integer>(arr.subList(0, num)); // sublist of arr
ArrayList<Integer> aLtemp = allSums(arrSublist); // aLtemp, created sending arrSublist into allSums
arrayListSum.addAll(aLtemp); // values listed within aLtemp are added to arrayListSum
int intEnd = arr.get(num); // last int in arr
/*
* for loop, the next end integer is selected and added to the integer of each
* ArrayList item. Then added into arrayListSum as new ArrayList items
*/
for (int i = 0; i < aLtemp.size(); i++) {
int temp = aLtemp.get(i); // current int in the ArrayList
arrayListSum.add(temp + intEnd); // new sum is being added
}
// else, when arr is empty, 0 is added and arrayListSum returned
} else {
arrayListSum.add(0);
return arrayListSum;
}
return arrayListSum;
}
public static void main(String[] args) {
// https://www.baeldung.com/java-file-to-arraylist
ArrayList<Integer> result = new ArrayList<Integer>(); // = Files.readAllLines(Paths.get("nums.txt"));
try {
BufferedReader br = new BufferedReader(new FileReader("nums.txt"));
while (br.ready()) {
int temp = Integer.parseInt(br.readLine());
result.add(temp);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
ArrayList<Integer> sums = allSums(result);
System.out.println(sums);
assertEquals(sums.size(), 8);
assertEquals(sums.contains(0), true);
assertEquals(sums.contains(1), true);
assertEquals(sums.contains(2), true);
assertEquals(sums.contains(4), true);
assertEquals(sums.contains(3), true);
assertEquals(sums.contains(5), true);
assertEquals(sums.contains(6), true);
assertEquals(sums.contains(7), true);
System.out.println("Success!");
}
}