-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVersion1.py
650 lines (593 loc) · 15.7 KB
/
Version1.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# Programmer: Jared Strand
# Platform: Windows
# global variables
opStack = []
dictStack = []
#--------------------------------
debug_level = 0
def debug(*s):
if debug_level > 0:
print(s)
#---------- Operand Stack Operators --------------
# Pop a value from the opStack
def opPop():
if len(opStack) > 0:
x = opStack[len(opStack) - 1]
opStack.pop(len(opStack) - 1)
return x
else:
debug("Error: opPop - Operand stack is empty")
# Pop a value from the opStack
def opPush(value):
opStack.append(value)
#---------- Dict Stack Operators ---------------
def dictPop():
if len(dictStack) > 0:
dictStack.pop(len(dictStack) - 1)
else:
debug("Error: dictPop - Dictionary stack is empty")
def dictPush():
d = {}
dictStack.append(d)
def define(name, value):
if (len(dictStack) > 0):
dictStack[len(dictStack) - 1][name] = value
else:
newDict = {}
newDict[name] = value
dictStack.append(newDict)
def lookup(name):
for d in reversed(dictStack):
if d.get('/' + name, None) != None:
return d.get('/' + name, None)
return None
#---------- Arithmetic and Comparison Operators --------------
def add():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)))))):
opPush(op1 + op2)
else:
debug("Error - Invalid Operands")
else:
debug("Error - Stack incorrect length")
def sub():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)))))):
opPush(op1 - op2)
else:
debug("Error - Invalid Operands")
else:
debug("Error - Stack incorrect length")
def mul():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)))))):
opPush(op1 * op2)
else:
debug("Error - Invalid Operands")
else:
debug("Error - Stack incorrect length")
def div():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)) and (op2 != 0))))):
opPush(op1 / op2)
else:
debug("Error - Invalid Operands")
else:
debug("Error - Stack incorrect length")
def eq():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if (op1 == op2):
opPush(True)
else:
opPush(False)
else:
debug("Error - Stack incorrect length")
def lt():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)))))):
if (op1 < op2):
opPush(True)
else:
opPush(False)
else:
debug("Error: Invalid Operands")
else:
debug("Error - Stack incorrect length")
def gt():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if ((isinstance(op1,int) or (isinstance(op1,float)) and
((isinstance(op2,int) or (isinstance(op2,float)))))):
if (op1 > op2):
opPush(True)
else:
opPush(False)
else:
debug("Error: Invalid Operands")
else:
debug("Error - Stack incorrect length")
# ---------- String Operators --------------
def length():
if (len(opStack) > 0):
string = opPop()
if (isinstance(string, str)):
opPush(len(string))
else:
debug("Error: Invalid String Operand")
else:
debug("Error - Stack incorrect length")
def get():
if (len(opStack) > 1):
index = opPop()
string = opPop()
if (index > (len(string) - 1)):
debug("Error: String index out of bounds")
return
if ((isinstance(string, str)) and ((isinstance(index, int)))):
opPush(string[index])
else:
debug("Error: Invalid String or Index Operand")
else:
debug("Error - Stack incorrect length")
def getinterval():
if (len(opStack) > 2):
count = opPop()
index = opPop()
string = opPop()
if (index > (len(string) - 1) or count > (len(string) - 1)):
debug("Error: String index or count out of bounds")
return
if ((index + count) > len(string)):
debug("Error: Counter out of range of string - cannot create substring with given length")
return
if ((isinstance(string, str)) and ((isinstance(index, int))) and (isinstance(count, int))):
substring = string[index:(index + count)]
opPush(substring)
else:
debug("Error: Invalid String or Index Operand")
else:
debug("Error - Stack incorrect length")
# ---------- Boolean Operators --------------
def psAnd():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if (((isinstance(op1,int) or (isinstance(op1,float)) and
(isinstance(op2, int) or (isinstance(op2, float))))) or
(isinstance(op1, bool) and isinstance(op2, bool))):
if (op1 == op2):
opPush(True)
else:
opPush(False)
else:
debug("Error: Type error")
else:
debug("Error - Stack incorrect length")
def psOr():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
if (((isinstance(op1, int) or (isinstance(op1, float)) and
(isinstance(op2, int) or (isinstance(op2, float))))) or
(isinstance(op1, bool) and isinstance(op2, bool))):
if (((op1 == True or op1 > 0) or (op2 == True or op2 > 0))):
opPush(True)
else:
opPush(False)
else:
debug("Error: Type error")
else:
debug("Error - Stack incorrect length")
def psNot():
if (len(opStack) > 0):
op1 = opPop()
if isinstance(op1, bool) or isinstance(op1, int):
if isinstance(op1, bool):
opPush(not op1)
else:
opPush(op1 - (2 * op1))
else:
debug("Error: Type error, ints and bools only")
else:
debug("Error - Stack incorrect length")
# ---------- Stack Manipulation and Print Operators --------------
def dup():
if (len(opStack) > 0):
op1 = opPop()
opPush(op1)
opPush(op1)
else:
debug("Error - Stack incorrect length")
def exch():
if (len(opStack) > 1):
op2 = opPop()
op1 = opPop()
opPush(op2)
opPush(op1)
else:
debug("Error - Stack incorrect length")
def pop():
if (len(opStack) > 0):
op1 = opPop()
else:
debug("Error - Stack incorrect length")
def roll():
if len(opStack) > 0:
n = opPop()
m = opPop()
#if n is postitive
#right rotate
if n > 0:
tempStack = (opStack[n:] + opStack[:n])
clear()
for i in tempStack:
opStack[i] = tempStack[i]
#if n is negative
#left rotate
if n < 0:
tempStack = opStack[n:] + opStack[:n]
clear()
for i in tempStack:
opStack[i] = tempStack[i]
else:
debug("Error: roll - n = 0")
#first idea
# if n > 0:
# copyList[:0] = copyList[m-n:]
# copyList[m - n:] = []
# elif n < 0:
#else:
# debug("Error: n = 0")
def copy():
if (len(opStack) > 0):
opList = [] # create copy list
for opIndex in opStack: #copy current stack list into temp list
opList[opIndex] = opStack[opIndex]
for opIndex2 in opList: #push copied list on top of original stack creating copy stack
opPush(opList[opIndex2])
else:
debug("Error - Stack already empty")
def clear():
if (len(opStack) > 0):
while len(opStack) != 0:
opPop()
else:
debug("Error - Stack already empty")
def stack():
if (len(opStack) > 0):
stacklist = reversed(opStack)
for i in stacklist:
print('\n' + stacklist[i])
else:
debug("Error: stack - not enough args")
# ---------- Dictionary Manipulation Operators --------------
def psDict():
if len(opStack) > 0:
opPop()
dictPush()
else:
debug("Error: psDict - not enough arguments")
def begin():
if len(opStack) > 0:
newDict = opPop()
dictPush()
else:
debug("Error: begin - not enough arguments")
def end():
if len(dictStack) > 0:
dictPop()
else:
debug("Error: end - not enough dictionaries to pop")
def psDef():
if len(opStack) > 1:
value = opPop()
name = opPop()
if isinstance(name, str):
define(name, value)
else:
debug("Error: psDef - invalid name argument")
else:
debug("Error: psDef - not enough arguments")
# ---------- TEST FUNCTIONS --------------
def testOpPop():
opPush(1)
opPush(1)
opPop()
opPop()
if len(opStack) != 0:
clear()
return False
elif len(opStack) == 0:
clear()
return True
def testOpPush():
opPush(1)
opPush(98)
if len(opStack) == 2 and opStack[1] == 98:
clear()
return True
else:
clear()
return False
def testDictPop():
#opPush('{}')
#begin()
opPush('/y')
opPush(5)
psDef()
dictPop()
if len(dictStack) == 0:
clear()
return True
else:
clear()
return False
def testDictPush():
dictPush()
dictPush()
dictPush()
if len(dictStack) == 3:
dictPop()
dictPop()
dictPop()
return True
else:
dictPop()
dictPop()
dictPop()
return False
def testDefine():
nam = '/testName'
val = 45
define(nam, val)
if dictStack[0][nam] == 45: #needs work
clear()
dictPop()
return True
else:
clear()
dictPop()
return False
def testLookup():
opPush('/y')
opPush(3)
psDef()
if lookup('y') != 3:
dictPop()
clear()
return False
elif lookup('y') == 3:
dictPop()
clear()
return True
def testAdd():
opPush(1)
opPush(2)
add()
if opPop() != 3:
clear()
return False
else:
clear()
return True
def testSub():
opPush(2)
opPush(2)
sub()
if opPop() != 0:
clear()
return False
else:
clear()
return True
def testMul():
opPush(1)
opPush(2)
mul()
if opPop() != 2:
clear()
return False
else:
clear()
return True
def testDiv():
opPush(4)
opPush(2)
div()
if opPop() != 2:
clear()
return False
else:
clear()
return True
def testEq():
opPush("hi")
opPush("hi") #also works with ints
eq()
if opPop() == True:
clear()
return True
else:
clear()
return False
def testLt():
opPush(1)
opPush(85)
lt()
if opPop() == True:
clear()
return True
else:
clear()
return False
def testGt():
opPush(56)
opPush(32)
gt()
if opPop() == True:
clear()
return True
else:
clear()
return False
def testLength():
opPush("Hello World") #len of string
length()
lengthofString = opPop()
if(len("Hello World") == lengthofString):
clear()
return True
else:
clear()
return False
def testGet():
opPush("Hello")
opPush(3)
get()
valAtIndex = opPop()
if valAtIndex == 'l':
clear()
return True
else:
clear()
return False
def testGetInterval():
opPush("hello world")
opPush(1)
opPush(4)
getinterval()
interval = opPop()
if interval == "ello":
clear()
return True
else:
clear()
return False
def testPsAnd():
opPush(True)
opPush(True)
psAnd()
value = opPop()
if value == True:
clear()
return True
else:
clear()
return False
def testPsOr():
opPush(True)
opPush(False)
psOr()
value = opPop()
if value == True:
clear()
return True
else:
clear()
return False
def testPsNot():
opPush(True)
psNot()
value = opPop()
if value == False:
clear()
return True
else:
clear()
return False
def testDup():
pass
# valueList = []
# opPush(1)
# opPush(2)
# dup()
# for i in opStack:
# valueList[i] = opStack[i]
#
# if valueList[0] == 1 and valueList[1] == 2:
# clear()
# return True
# else:
# clear()
# return False
def testExch():
pass
def testPop():
pass
def testRoll():
pass
def testCopy():
pass
def testClear():
pass
def testStack():
pass
def testPsDict():
pass
def testBegin():
pass
def testEnd():
pass
def testPsDef():
opPush("/y")
opPush(3)
psDef()
if lookup("y") != 3: return False
return True
# ---------- MAIN FUNCTION --------------
def main_part1():
testCases = [('opPop', testOpPop),
('opPush', testOpPush),
('dictPop', testDictPop),
('dictPush', testDictPush),
('define', testDefine),
('lookup', testLookup),
('add', testAdd),
('sub', testSub),
('mul', testMul),
('div', testDiv),
('eq', testEq),
('lt', testLt),
('gt', testGt),
('length', testLength),
('get', testGet),
('getinterval', testGetInterval),
('psAnd', testPsAnd),
('psOr', testPsOr),
('psNot', testPsNot),
('dup', testDup),
('exch', testExch),
('pop', testPop),
('roll', testRoll),
('copy', testCopy),
('clear', testClear),
('stack', testStack),
('psDict', testPsDict),
('begin', testBegin),
('end', testEnd),
('psDef', testPsDef)]
failedTests = [testName for (testName, testProc) in testCases if not testProc()]
if failedTests:
return ('Some tests failed', failedTests, 'not all tests implemented')
else:
return ('All tests OK')
if __name__ == '__main__':
print(main_part1())