-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
42 lines (32 loc) · 1012 Bytes
/
stack.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
class stack:
def __init__(self,*,size):
self.size = size
self.stack = []
def push(self, value):
assert len(self.stack)+1 <= self.size
# print(self.stack)
self.stack.append(value)
def top(self):
assert len(self.stack) != 0
return self.stack[-1]
def second(self):
assert len(self.stack)-1 != 0
return self.stack[-2]
def pop(self):
# assert len(self.stack) != 0
return self.stack.pop()
def set_value(self, index, value):
assert len(self.stack)+1 <= self.size
self.stack[index] = value
def set_top(self, value):
assert len(self.stack) != 0
self.stack[-1] = value
def set_second(self, value):
assert len(self.stack)-1 != 0
self.stack[-2] = value
def peek(self,n):
return self.stack[-n]
def __repr__(self):
return str(self.stack)
def __len__(self):
return len(self.stack)