-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_parenthesis.py
35 lines (34 loc) · 1.1 KB
/
check_parenthesis.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
def stack_push(stack,element):
stack.append(element)
def check_parentesis(pattern):
stack = []
bracket1 = ['(', ')']
bracket2 = ['{', '}']
bracket3 = ['[', ']']
start_bracket = ['(', '{', '[']
for element in pattern:
if element in start_bracket:
stack_push(stack,element)
print(stack)
else:
if len(stack) == 0:
print("invalid")
else:
element1 = element
for i in range(len(stack)-1,-1,-1):
if stack[i] in start_bracket:
matching_bracket = stack[i]
break
if element1 in bracket1:
open_bracket = bracket1[0]
elif element1 in bracket2:
open_bracket = bracket2[0]
elif element1 in bracket3:
open_bracket = bracket3[0]
if open_bracket == matching_bracket:
stack.pop()
if len(stack) != 0:
print("invalid")
else:
print("valid")
check_parentesis("[(])")