forked from magedu/python2016
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
39 lines (33 loc) · 911 Bytes
/
stack.py
File metadata and controls
39 lines (33 loc) · 911 Bytes
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
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self.top = None
def push(self, value):
node = Node(value)
node.next = self.top
self.top = node
def pop(self):
node = self.top
self.top = node.next
return node.value
if __name__ == '__main__':
stack = Stack()
exp = '({a * [x/(x+y)]}'
for c in exp:
if c in '{[(':
stack.push(c)
elif c in '}])':
v = stack.top.value
if c == '}' and v != '{':
raise Exception('failed')
if c == ']' and v != '[':
raise Exception('failed')
if c == ')' and v != '(':
raise Exception('failed')
stack.pop()
if stack.top is not None:
raise Exception('failed')
print("ok")