-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
70 lines (50 loc) · 1.68 KB
/
Copy pathtest.java
File metadata and controls
70 lines (50 loc) · 1.68 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
package test;
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runners.JUnit4;
class DirReduction {
public static String[] dirReduc(String[] arr) {
Deque<String> tem = new ArrayDeque<>();
for(int i=0; i<arr.length;i++){
String string = arr[i];
if (string.equals("NORTH")) {
if( tem.peekLast()=="SOUTH") {
tem.removeLast();
}else tem.add(arr[i]);
} else if (string.equals("SOUTH")) {
if( tem.peekLast()=="NORTH") {
tem.removeLast();
}else
tem.add(arr[i]);
} else if (string.equals("WEST")) {
if( tem.peekLast()=="EAST" ) {
tem.removeLast();
}else
tem.add(arr[i]);
} else if (string.equals("EAST")) {
if( tem.peekLast()=="WEST" ) {
tem.removeLast();
}else
tem.add(arr[i]);
} else {
}
}
String[] dir = tem.toArray(new String[tem.size()]);
return dir;
}
}
public class test {
@Test
public static void testSimpleDirReduc() {
assertArrayEquals("\"NORTH\", \"SOUTH\", \"SOUTH\", \"EAST\", \"WEST\", \"NORTH\", \"WEST\"",
new String[]{"WEST"},
DirReduction.dirReduc(new String[]{"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"}));
assertArrayEquals("\"NORTH\",\"SOUTH\",\"SOUTH\",\"EAST\",\"WEST\",\"NORTH\"",
new String[]{},
DirReduction.dirReduc(new String[]{"NORTH","SOUTH","SOUTH","EAST","WEST","NORTH"}));
}
public static void main(String[] args) {
testSimpleDirReduc();
}
}