-
Notifications
You must be signed in to change notification settings - Fork 3
/
Stackd.py
90 lines (69 loc) · 2.12 KB
/
Stackd.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
#Stackd library for Keg
def _ord(char):
if char in code_page:
return code_page.find(char)
return ord(char)
code_page = "Set me in Keg.py"
input_raw = False #Set me in Keg.py using flags
class Stack:
def __init__(self, iterable=()):
if iterable:
self.__stack = list(iterable)
self.stacks = self.__stack.copy()
else:
self.__stack = []
self.stacks = [self.__stack]
self.level = 0
self.inputs = []
def __len__(self):
return len(self.__stack)
def __setitem__(self, index, value):
self.__stack[index] = value
def __getitem__(self, index):
return self.__stack[index]
def __delitem__(self, what):
del self.__stack[what]
def push(self, value):
self.__stack.append(value)
def pop(self):
if self.__stack:
return self.__stack.pop()
try:
temp = input()
except:
return None
if input_raw:
from Coherse import char
for Char in reversed(temp):
self.__stack.append(char(Char))
return self.__stack.pop()
try:
if type(eval(temp)) is float:
temp = float(temp)
elif type(eval(temp)) is int:
temp = int(temp)
elif type(eval(temp)) is list:
temp = Stack(eval(temp))
elif type(eval(temp)) is str:
for Char in reversed(temp):
self.__stack.append(Char)
return self.__stack.pop()
except:
temp = temp
self.inputs.append(temp)
return temp
def __repr__(self):
return str(self.__stack)
def clear(self):
self.__stack.clear()
def index(self, *pos_list):
#Iteratively index the stack
temp = self.__stack
for indx in pos_list:
temp = temp[indx]
return temp
def __str__(self):
return "".join([str(item) for item in self.__stack])
if __name__ == "__main__":
x = Stack([1, 2, 3, [4, 5, [6, 7, 8]]])
print(x.index(3, 2, 2))