-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
374 lines (347 loc) · 9.64 KB
/
helper.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import random
def getScramble(length):
"""
Generates a scramble string.
Parameters
----------
length : string
Length of the scramble string to be created.
Returns
-------
scr : string
A string that has the scramble of the given length.
Examples
--------
>>> getScramble(10)
"RLB'F'LU'RDL'F'"
"""
vMoves = ['U', 'D', 'R', 'L', 'F', 'B'] # 'Uw', 'Dw', 'Rw', 'Lw', 'Fw', 'Bw', 'E', 'M', 'S', 'x', 'y', 'z']
scr = ""
for _ in range(length):
scr += vMoves[int(random.random() * len(vMoves))]
if(random.random() > 0.7):
scr += '\''
scr = condenseFormula(scr)
return scr
def condenseFormula(form, advanced=True):
"""
Condenses a forumla.
Parameters
----------
form : string
The formula to be condensed.
advanced : bool, default=True
If set to True, it will support multi level paranthesis reduction and condensation.
Enabling this will make it much slower.
If set to False, it is the same as rawCondense()
Returns
-------
ans : string
The condensed form of the given formula.
Examples
--------
>>> condenseFormula("RUUFB'B'")
"RU2FB'2"
>>> condenseFormula("(RUU)(RUU)")
'(RU2)2'
"""
if(not isValid(form)):
return "ERROR"
if(not advanced):
return rawCondense(form)
ans = ""
tmp = ""
for ch in form:
if(ch == '(' or ch == ')'):
if(len(tmp) > 0):
ans += rawCondense(tmp)
tmp = ""
ans += ch
else:
tmp += ch
if(len(tmp) > 0):
ans += rawCondense(tmp)
maxlevel = getMaxLevel(ans)
for level in range(maxlevel, 0, -1):
ans = parCondense(ans, level)
return ans
def isValid(form):
"""
Checks the structural and symbol validity of the forumla.
Parameters
----------
form : string
The formula to be validated.
Returns
-------
valid : bool
Whether the formula is valid or not.
Examples
--------
>>> isValid("RUR'U'")
True
>>> isValid("RUR'UG")
False
>>> isValid("(RU)((F2)2)")
True
>>> isValid("((DU")
False
"""
level = 0
valid = True
validAlpha = ['U', 'D', 'R', 'L', 'F', 'B', 'E', 'M', 'S', 'x', 'y', 'z', 'u', 'd', 'r', 'l', 'f', 'b', 'w']
boolAlpha = False
boolPrime = False
boolDec = False
for ch in form:
if(ch == '('):
level += 1
boolAlpha = False
boolPrime = False
boolDec = False
elif(ch == ')'):
if(level > 0):
level -= 1
else:
valid = False
boolAlpha = True
boolPrime = False
boolDec = False
else:
if(ch in validAlpha):
boolAlpha = True
boolPrime = False
boolDec = False
elif((ch == '\'' or ch == 'P') and boolAlpha and not boolPrime and not boolDec):
if(boolDec):
boolAlpha = False
boolDec = False
else:
boolPrime = True
elif(ch.isdigit() and boolAlpha):
boolDec = True
else:
valid = False
if(level != 0):
valid = False
return valid
def getMaxLevel(form):
"""
Calculates the max depth of the paranthesis.
Parameters
----------
form : string
Formula.
Returns
-------
maxlevel : int
The max depth of the paranthesis.
"""
level = 0
maxlevel = 0
for ch in form:
if(ch == '('):
level += 1
if(level > maxlevel):
maxlevel = level
elif(ch == ')'):
level -= 1
return maxlevel
def parCondense(form, tar):
"""
Performs paranthesis reduction at a particular depth.
Parameters
----------
form : string
Formula.
tar : int
Target depth for paranthesis condensation.
Returns
-------
ans : string
The condensed paranthesis form of the given formula.
"""
form += '@'
ans = ""
temp = ""
ref = ""
refctr = 0
ctr = 0
for ch in form:
if(ch == '('):
ctr += 1
if(ctr >= tar):
temp += ch
else:
if(len(ref) > 0):
ans += ref
ans += str(refctr) if refctr > 1 else ""
ref = ""
refctr = 0
ans += ch
if(ch == ')'):
if(ctr == tar):
if(temp == ref):
refctr += 1
else:
ans += ref
ans += str(refctr) if refctr > 1 else ""
ref = temp
refctr = 1
temp = ""
ctr -= 1
ans = ans[:-1]
return ans
def rawCondense(form):
"""
Condenses a forumla. Does not support paranthesis. Does not perform validity as it is a core function.
Parameters
----------
form : string
The formula to be condensed.
The formula should be native and valid.
Returns
-------
cform : string
The condensed form of the given formula.
Examples
--------
>>> rawCondense("RUUFB'B'")
"RU2FB'2"
"""
if(form.isdigit()):
return form
# string to 2d count array
temp = []
for i, item in enumerate(form):
if(form[i].isalpha() and not form[i] == 'P' and not form[i] == 'w'):
temp.append([form[i], ""])
else:
if(form[i].isdigit()):
temp[-1][1] += form[i]
else:
temp[-1][0] += form[i]
# int() of count
for i, item in enumerate(temp):
if(temp[i][1] == ""):
temp[i][1] = 1
else:
temp[i][1] = int(temp[i][1])
# removing anti moves and combining same moves
while True:
isChange = False
for i in range(len(temp) - 1):
if(isPrimePair(temp[i][0], temp[i + 1][0])):
minv = min(temp[i][1], temp[i + 1][1])
temp[i][1] -= minv
temp[i + 1][1] -= minv
if(temp[i + 1][1] == 0):
temp.pop(i + 1)
if(temp[i][1] == 0):
temp.pop(i)
isChange = True
break
elif(temp[i][0] == temp[i + 1][0]):
temp[i][1] += temp[i + 1][1]
temp.pop(i + 1)
isChange = True
break
elif(temp[i][1] % 4 == 0):
temp.pop(i)
isChange = True
break
if(not isChange):
break
# limit count to 2 and inverse moves for 3
for i, item in enumerate(temp):
temp[i][1] = ((temp[i][1] - 1) % 4) + 1
if(temp[i][1] == 3):
if(temp[i][0][-1] == "\'"):
temp[i][0] = temp[i][0][:-1]
else:
temp[i][0] += "\'"
temp[i][1] = 1
elif(temp[i][1] == 4):
temp[i][1] = 0
# 2d count array to string
cform = ""
for i, item in enumerate(temp):
if(temp[i][1] > 0):
cform += temp[i][0]
if(temp[i][1] == 2):
cform += '2'
return cform
def isPrimePair(s1, s2):
"""
Checks if the two moves are primes of each other assuming both are simple moves.
"""
if(len(s1) >= len(s2)):
a = s1
b = s2
else:
a = s2
b = s1
if(len(a) - len(b) == 1):
if(a[:len(b)] == b and a[-1] == "\'"):
return True
return False
def parseFormula(form, condense = True):
"""
Parses a complex formula into cube object understandable instructions.
Parameters
----------
form : string
The formula to be parsed.
condense : bool, default=True
If set to True, it will perform condensation to the formula before parsing.
This will skip redundant moves if present.
Returns
-------
ans : list of strings
List of instructions that the cube object can understand.
Empty list if the formula is invalid.
Examples
--------
>>> parseFormula("FRUR'URU2R'U")
['F', 'R', 'U', 'RP', 'U', 'R', 'U', 'U', 'RP', 'U']
>>> parseFormula("FRU(")
[]
"""
if(not isValid(form)):
return []
if(condense):
form = condenseFormula(form)
moves = [ch for ch in form]
vwMoves = ['U', 'D', 'R', 'L', 'F', 'B']
# Convert w moves to base moves
for i, item in enumerate(moves):
if(moves[i] == 'w'):
moves.pop(i)
if(i > 0 and moves[i - 1] in vwMoves):
moves[i - 1] = moves[i - 1].lower()
# Convert outprimes to base moves
vMoves = ['U', 'D', 'R', 'L', 'F', 'B', 'E', 'M', 'S', 'x', 'y', 'z', 'u', 'd', 'r', 'l', 'f', 'b']
cvm = -1
for i, item in enumerate(moves):
if(moves[i] in vMoves):
cvm = i
if(moves[i] == '\'' or moves[i] == 'P'):
moves.pop(i)
if(cvm >= 0):
moves.insert(cvm + 1, "P")
cvm = -1
# Converting the characters into move blocks
ans = []
for i, item in enumerate(moves):
if(moves[i] in vMoves):
cm = moves[i]
ctr = 1
if(i + 1 < len(moves) and moves[i + 1] == 'P'):
cm += 'P'
ctr = 2
cnt = 1
if(i + ctr < len(moves) and moves[i + ctr] >= '0' and moves[i + ctr] <= '9'):
cnt = int(moves[i + ctr])
for _ in range(cnt):
ans.append(cm)
return ans