-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackApplication.py
More file actions
40 lines (32 loc) · 858 Bytes
/
Copy pathStackApplication.py
File metadata and controls
40 lines (32 loc) · 858 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
40
#this program demonstrate the practical implemetation of Stack.
# In a bracket balance check application
# stack are use at number of places other than these mainly:
# Function calls
# Recursion
# Browser Back and forward button
from stack import Stack
def check_brackets(statement):
stack = Stack()
for ch in statement:
if ch in ('{','[','('):
stack.push(ch)
elif ch in ('}',']',')'):
last = stack.pop()
if last == '{' and ch == '}':
continue
elif last == '[' and ch == ']':
continue
elif last == '(' and ch == ')':
continue
else:
return False
if stack._Stack__size == 0:
return True
else:
return False
s1 = ("{(foo)(bar)}[hello](((this)is)a)test",
"{(foo)(bar)}[hello](((this)is)atest",
"{(foo)(bar)}[hello](((this)is)a)test))")
for s in s1:
m = check_brackets(s)
print("{}: {}".format(s,m))