-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Answers.java
More file actions
110 lines (98 loc) · 3.54 KB
/
Find_Answers.java
File metadata and controls
110 lines (98 loc) · 3.54 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.ArrayList;
/* Module FindAnswers: searches through the character array from the file,
and for each problem, finds the starting and ending points of each answer.
The answer includes "\item" and ends just before the next "\item" or
"\end{enumerate}". */
public class FindAnswers {
//Answer Keywords
public final static char[] beginEnumerate =
"\\begin{enumerate}".toCharArray();
public final static char[] item = "\\item".toCharArray();
public final static char[] endEnumerate =
"\\end{enumerate}".toCharArray();
/* The chars array generated by Input module (in) */
char[] contents;
/* Start index of each problem, from module FindProblems (in) */
int[] probStarts;
/* Stop index of each problem, from module FindProblems (in) */
int[] probStops;
/* For each problem i, start index of each answer to the problem
(out). This is an array of length number of problems.
answerStarts[i] is an array whose length is the number of
answers to the i-th problem. answerStarts[i][j] is the index
in chars of the first character of the j-th answer to the i-th
problem. */
int[][] answerStarts;
/* Like answerStarts, except it gives the stop index of each
answer (out). That is 1 greater than the index of the last
character of the answer. */
int[][] answerStops;
/* Constructs new FindAnswers instance from the given data. Sets
the fields and does nothing else. */
public FindAnswers(char[] contents, int[] probStarts, int[] probStops) {
this.contents = contents;
this.probStarts = probStarts;
this.probStops = probStops;
}
/* Tells you whether the sequence of chars starting at position in
array chars matches those of c, now its own module.
private boolean match(int off, char[] c) {
int n = c.length;
if (off+n > chars.length)
return false;
for (int i=0; i<n; i++) {
if (c[i] != chars[off+i])
return false;
}
return true;
}
*/
/* Converts an array list of Integer to an array of int. */
private static int[] toArray(ArrayList<Integer> list) {
int n = list.size();
int[] result = new int[n];
for (int i=0; i<n; i++)
result[i] = list.get(i);
return result;
}
/* Finds the answers to the problem with ID pid, setting
answerStarts[pid] and answerStops[pid]. */
private void findAnswersInProblem(int pid) {
ArrayList<Integer> startList = new ArrayList<>(),
stopList = new ArrayList<>();
int i = probStarts[pid];
int stop = probStops[pid];
matchBeg = new Match_Answers(i, beginEnumerate, contents);
for (; i < stop && !matchBeg.execute; i++) ;
if (i == stop)
throw new RuntimeException
("No \\begin{enumerate} found for problem "+pid);
for (; i < stop; i++) {
matchEnd = new Match_Answers(i, endEnumerate, contents);
if (matchEnd.execute()) {
if (!startList.isEmpty()) stopList.add(i);
break;
}
matchEnd = new Match_Answers(i, item, contents);
if (matchEnd.execute()) {
if (!startList.isEmpty()) stopList.add(i);
startList.add(i);
}
}
if (i == stop)
throw new RuntimeException
("No \\end{enumerate} found for problem "+pid);
int nanswer = startList.size();
assert nanswer == stopList.size();
answerStarts[pid] = toArray(startList);
answerStops[pid] = toArray(stopList);
}
/* Constructs answerStarts and answerStops. */
public void execute() {
int nprob = probStarts.length;
answerStarts = new int[nprob][];
answerStops = new int[nprob][];
for (int i=0; i<nprob; i++)
findAnswersInProblem(i);
}
}