-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilpPancake.java
More file actions
198 lines (178 loc) · 5.43 KB
/
FilpPancake.java
File metadata and controls
198 lines (178 loc) · 5.43 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
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
public class FilpPancake {
private static int ctflip = 0;
private static linkedList<Integer> flips = new linkedList<Integer>();
public static void main(String[] args) {
String dtype = args[0];
int num = Integer.parseInt(args[1]);
if(dtype.equals("array")){
ArrayStack<Integer> main = new ArrayStack<Integer>();
int lastindex = args.length -1;
for(int i = lastindex ;i>=2;i--){
main.push(Integer.parseInt(args[i]));
}
main.print();
sort(main, num);
System.out.print("("+ctflip+") ");
System.out.print(flips.toString());
System.out.println();
main.print();
}
else if(dtype.equals("list")){
ListStack<Integer> Lmain = new ListStack<Integer>();
int lastindex = args.length -1;
for(int i = lastindex ;i>=2;i--){
Lmain.push(Integer.parseInt(args[i]));
}
Lmain.print();
Lsort(Lmain, num);
System.out.print(" ("+ctflip+") ");
System.out.print(flips.toString());
System.out.println();
Lmain.print();
}
}
// <--------------------Array stack methods----------------------->
private static void flip(ArrayStack<Integer> S, int idx) {
ArrayStack<Integer> temp = new ArrayStack<Integer>();
ArrayStack<Integer> temp2 = new ArrayStack<Integer>();
int c = S.size() - idx;
for (int i = 0; i < c; i++) { //pop out size - index elements
temp.push(S.pop() * -1); //each time flip, the elements turn from positive to negative or negative to positive
}
while (!temp.isEmpty()) { //push to temp2 to adjust the order
temp2.push(temp.pop());
}
while (!temp2.isEmpty()) { //push back to S so the flip is done
S.push(temp2.pop());
}
ctflip++;
flips.AddBack(c);
//System.out.print(c + " ");
}
private static void sort(ArrayStack<Integer> S, int idx) {
for (int size = 0; size < idx; size++) {
int max = findMax(S, size); //get position of the biggest element from current size to the top
if (max != size || isBurned(S, max)) { //do fliping if max position is not the current size or the element is burned
if (max != S.size() - 1) { //if max element is not at the bottom
flip(S, max); //flip the pancakes from max position
//S.print();
}
if (S.top() > 0) { //if top is positive
flip(S, S.size() - 1); //flip itself to negative
//S.print();
}
flip(S, size); //flips the pancakes from size position that moves the current top to size position
}
//S.print();
}
}
private static int findMax(ArrayStack<Integer> S, int idx) {
ArrayStack<Integer> temp = new ArrayStack<Integer>();
int counter = idx; //find max element from idx to the top
int max = 0;
int position = idx - 1;
int n = S.size() - idx;
for (int i = 0; i < n; i++) {
temp.push(S.pop());
}
// temp.print();
while (!temp.isEmpty()) { // push back the element
if (Math.abs(temp.top()) > max) {
max = Math.abs(temp.top());
position = counter;
}
S.push(temp.pop());
counter++;
}
//S.print();
return position;
}
private static boolean isBurned(ArrayStack<Integer> S, int idx) {
ArrayStack<Integer> temp = new ArrayStack<Integer>();
int c = S.size() - idx;
for (int i = 0; i < c; i++) { //pop out size - index elements
temp.push(S.pop());
}
int tempN = temp.top(); //save the target element
while (!temp.isEmpty()) {
S.push(temp.pop());
}
if (tempN > 0) { //determinate the burned side
return false;
} else {
return true;
}
}
//<------------------LinkStack methods-------------------------->
// the functions are the same as array version but they accept ListStack and use ListStack as temporary storage
private static void Lflip(ListStack<Integer> S, int idx) {
ListStack<Integer> temp = new ListStack<Integer>();
ListStack<Integer> temp2 = new ListStack<Integer>();
int c = S.size() - idx;
for (int i = 0; i < c; i++) {
temp.push(S.pop() * -1);
}
while (!temp.isEmpty()) {
temp2.push(temp.pop());
}
while (!temp2.isEmpty()) {
S.push(temp2.pop());
}
ctflip++;
flips.AddBack(c);
//System.out.print(c + " ");
}
private static void Lsort(ListStack<Integer> S, int idx) {
for (int size = 0; size < idx; size++) {
int max = LfindMax(S, size); // 2
if (max != size || LisBurned(S, max)) {
if (max != S.size() - 1) {
Lflip(S, max);
//S.print();
}
if (S.top() > 0) {
Lflip(S, S.size() - 1);
//S.print();
}
Lflip(S, size);
}
//S.print();
}
}
private static int LfindMax(ListStack<Integer> S, int idx){
ListStack<Integer> temp = new ListStack<Integer>();
int counter = idx;
int max = 0;
int position = idx - 1;
int n = S.size() - idx;
for (int i = 0; i < n; i++) {
temp.push(S.pop());
}
//temp.print();
while (!temp.isEmpty()) { // push back the element
if (Math.abs(temp.top()) > max) {
max = Math.abs(temp.top());
position = counter;
}
S.push(temp.pop());
counter++;
}
return position;
}
private static boolean LisBurned(ListStack<Integer> S, int idx) {
ListStack<Integer> temp = new ListStack<Integer>();
int c = S.size() - idx;
for (int i = 0; i < c; i++) {
temp.push(S.pop());
}
int tempN = temp.top();
while (!temp.isEmpty()) {
S.push(temp.pop());
}
if (tempN > 0) {
return false;
} else {
return true;
}
}
}