-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidterm.java
358 lines (317 loc) · 11.5 KB
/
Midterm.java
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package midterm;
import java.util.Stack;
public class Midterm {
public static void main(String[] args) {
// Test Todo - RandomAccessFile & OOP
Todo todo1 = new Todo(0, "First todo", "Todo No. 1", true);
Todo todo2 = new Todo(1, "Second todo", "Todo No. 2", false);
TodoDB.writeToFile(todo1);
TodoDB.writeToFile(todo2);
TodoDB.readFromFile();
TodoDB.readAllTitle();
System.out.println("----------------------------------");
// Test SinglyLinkedList
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.addToHead(10);
singlyLinkedList.addToTail(20);
singlyLinkedList.addToTail(30);
singlyLinkedList.deleteFromHead();
singlyLinkedList.deleteFromTail();
singlyLinkedList.addToTail(40);
singlyLinkedList.addAt(1, 30);
singlyLinkedList.deleteAt(1);
singlyLinkedList.printAllNode();
System.out.println("----------------------------------");
// Test DoublyLinkedList
DoublyLinkedList doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.addToHead(10);
doublyLinkedList.addToTail(20);
doublyLinkedList.addToTail(30);
doublyLinkedList.deleteFromHead();
doublyLinkedList.deleteFromTail();
doublyLinkedList.addToTail(40);
doublyLinkedList.addAt(1, 30);
doublyLinkedList.deleteAt(1);
doublyLinkedList.printAllNode();
System.out.println("----------------------------------");
// Test SparseTable
// [SKIP]
System.out.println("----------------------------------");
// Test Stack
MyStack stack = new MyStack(10);
stack.push(10);
System.out.println(stack.pop());
stack.push(20);
stack.peek();
stack.size();
stack.clear();
stack.pop();
System.out.println("----------------------------------");
// Test Queue
MyQueue queue = new MyQueue(10);
queue.enqueue(0);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.enqueue(6);
queue.enqueue(7);
queue.enqueue(8);
queue.enqueue(9);
System.out.println(queue.dequeue());
queue.enqueue(10);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println("----------------------------------");
// Test Matching Delimeter
System.out.println(delemiterChecker("()()()()"));
System.out.println(delemiterChecker("((){})"));
System.out.println(delemiterChecker("2+12+23232+(23232+023232)"));
System.out.println(delemiterChecker("(([){})]"));
System.out.println("----------------------------------");
// Test Maze
int[][] maze = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
{9, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 2, 1, 0, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
mazeSolver(maze, new Position(8, 3));
System.out.println("----------------------------------");
// Test Long Number Addition
System.out.println(longNumberAddition("99", "1"));
System.out.println(longNumberAddition("143", "60"));
System.out.println(longNumberAddition("99", "600"));
System.out.println(longNumberAddition("99999", "111"));
System.out.println("----------------------------------");
// Test Recursion
printOneToN(10);
System.out.println("");
printNToOne(10);
System.out.println("");
printOddFirstTime(20);
System.out.println("");
printOddFirstTime(21);
System.out.println("");
System.out.println(fibonacci(10));
System.out.println(division(10, 2));
System.out.println(exponential(10, 2));
System.out.println(logarithm(32, 2));
System.out.println("----------------------------------");
}
public static boolean delemiterChecker(String str) {
Stack<String> st = new Stack<>();
for (int i = 0; i < str.length(); i++) {
String s = str.substring(i, i + 1);
if (s.equals("(") || s.equals("[") || s.equals("{") || s.equals("<")) {
st.push(s);
} else if (s.equals(")") || s.equals("]") || s.equals("}") || s.equals(">")) {
if (st.empty()) {
return false;
}
String sCheck = st.pop();
switch (s) {
case ")":
if (!sCheck.equals("(")) {
return false;
}
break;
case "]":
if (!sCheck.equals("[")) {
return false;
}
break;
case "}":
if (!sCheck.equals("{")) {
return false;
}
break;
case ">":
if (!sCheck.equals("<")) {
return false;
}
}
}
}
return true;
}
public static void mazeSolver(int[][] maze, Position startingPoint) {
// 0 = can walk, 1 = wall, 2 = tried block, 3 = non-tried block (pushed, but haven't try), 9 = exit
Position mouse = startingPoint;
Stack<Position> backtrack = new Stack<>();
backtrack.push(mouse);
while (maze[mouse.row][mouse.col] != 9) {
// See around if it can walk through
if (maze[mouse.row - 1][mouse.col] == 0) {
backtrack.push(new Position(mouse.row - 1, mouse.col));
maze[mouse.row - 1][mouse.col] = 3;
}
if (maze[mouse.row][mouse.col + 1] == 0) {
backtrack.push(new Position(mouse.row, mouse.col + 1));
maze[mouse.row][mouse.col + 1] = 3;
}
if (maze[mouse.row + 1][mouse.col] == 0) {
backtrack.push(new Position(mouse.row + 1, mouse.col));
maze[mouse.row + 1][mouse.col] = 3;
}
if (maze[mouse.row][mouse.col - 1] == 0) {
backtrack.push(new Position(mouse.row, mouse.col - 1));
maze[mouse.row][mouse.col - 1] = 3;
}
// See around if it is exit
if (maze[mouse.row - 1][mouse.col] == 9) {
System.out.println("Found exit at " + (mouse.row - 1) + " " + mouse.col);
break;
} else if (maze[mouse.row][mouse.col + 1] == 9) {
System.out.println("Found exit at " + mouse.row + " " + (mouse.col + 1));
break;
} else if (maze[mouse.row + 1][mouse.col] == 9) {
System.out.println("Found exit at " + (mouse.row + 1) + ", " + mouse.col);
break;
} else if (maze[mouse.row][mouse.col - 1] == 9) {
System.out.println("Found exit at " + mouse.row + " " + (mouse.col - 1));
break;
}
Position out = backtrack.pop();
mouse.row = out.row;
mouse.col = out.col;
maze[mouse.row][mouse.col] = 2;
}
}
public static String longNumberAddition(String input1, String input2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
for (int i = 0; i < input1.length(); i++) {
stack1.push(Integer.parseInt(input1.substring(i, i + 1)));
}
for (int i = 0; i < input2.length(); i++) {
stack2.push(Integer.parseInt(input2.substring(i, i + 1)));
}
String result = "";
int carry = 0;
while (!stack1.empty() && !stack2.empty()) {
int calculated = stack1.pop() + stack2.pop() + carry;
if (carry == 1) {
carry = 0;
}
if (calculated > 9) {
calculated %= 10;
carry = 1;
}
result = calculated + result;
}
while (!stack1.empty()) {
int calculated = stack1.pop() + carry;
if (carry == 1) {
carry = 0;
}
if (calculated > 9) {
calculated %= 10;
carry = 1;
}
result = calculated + result;
}
while (!stack2.empty()) {
int calculated = stack2.pop() + carry;
if (carry == 1) {
carry = 0;
}
if (calculated > 9) {
calculated %= 10;
carry = 1;
}
result = calculated + result;
}
if (stack1.empty() && stack2.empty() && carry == 1) {
result = 1 + result;
}
return result;
}
public static void printOneToN(int n) {
if (n > 0) {
printOneToN(n - 1);
System.out.print(n + " ");
}
}
public static void printNToOne(int n) {
if (n > 0) {
System.out.print(n + " ");
printNToOne(n - 1);
}
}
public static void printOddAscending(int n) {
if (n > 0) {
if (n % 2 != 0) {
printOddAscending(n - 2);
System.out.print(n + " ");
} else {
printOddAscending(n - 1);
}
}
}
public static void printOddFirstTime(int n) {
if (n % 2 == 0) {
printOddAscendingVersionTwo(n - 1);
} else {
printOddAscendingVersionTwo(n);
}
}
public static void printOddAscendingVersionTwo(int n) {
if (n > 0) {
printOddAscendingVersionTwo(n - 2);
System.out.print(n + " ");
}
}
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1 || n == 2) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
public static int division(int a, int b) {
if (a >= b) {
return 1 + division(a - b, b);
} else {
return 0;
}
}
public static int exponentialRecursion(int a, int b) {
if (b > 0) {
return a * exponentialRecursion(a, b - 1);
} else {
return 1;
}
}
public static double exponential(int a, int b) {
if (b < 0) {
return 1.0 / exponentialRecursion(a, -b);
} else {
return exponentialRecursion(a, b);
}
}
public static int logarithm(int n, int base) {
if (n > 1) {
return 1 + logarithm(n / base, base);
} else {
return 0;
}
}
}