-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
54 lines (44 loc) · 1.26 KB
/
stack.py
File metadata and controls
54 lines (44 loc) · 1.26 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
# author: Daniel Lozano
class Stack:
def __init__(self):
self._stack = []
self._long = 0
self._eliminates_values = []
self._undo = []
self._sum = 0
def push(self, value):
self._sum += value
self._stack.append(value)
self._long += 1
self._undo.append(0)
def pop(self):
self._sum -= self._stack[-1]
self._long -= 1
self._undo.append(1)
self._eliminates_values.append(self._stack[-1])
self._stack.pop()
def peek(self):
return self._stack[-1]
def empty(self):
return self._long == 0
def clear(self):
self._stack = []
self._long = 0
self._eliminates_values = []
self._undo = []
def undo(self):
if self._undo[-1] == 0:
self._sum -= self._stack[-1]
self._undo.pop()
self._stack.pop()
self._long -= 1
elif self._undo[-1] == 1:
self._sum += self._eliminates_values[-1]
self._undo.pop()
self._stack.append(self._eliminates_values[-1])
self._eliminates_values.pop()
self._long += 1
def get_sum(self):
return self._sum
def get_long(self):
return self._long