-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParanthesisChecker.py
46 lines (34 loc) · 979 Bytes
/
ParanthesisChecker.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
from Stack import Stack
def par_checker(symbol_string):
stack = Stack()
balanced = True
index = 0
while index < len(symbol_string) and balanced:
symbol = symbol_string[index]
if symbol in "({[":
stack.push(symbol)
else:
if stack.is_empty():
balanced = False
else:
sym = stack.pop()
if not matches(sym,symbol):
balanced = False
index = index + 1
if balanced and stack.is_empty():
return True
else:
return False
def matches(openp, closep):
opens="[{("
closes="]})"
return opens.index(openp) == closes.index(closep)
def rev_String(String):
stack = Stack()
for char in String:
stack.push(char)
while not stack.is_empty():
print(stack.pop()+" ")
rev_String("balaji")
print(par_checker("()[]"))
print(par_checker('()([])'))