-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-5.py
47 lines (36 loc) · 1.02 KB
/
Problem-5.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
45
46
47
'''
Valid Paranthesis
example -
() -> True
([]) -> True
([)] -> False
([{}]) -> True
[ -> False
'''
input_1 = '()'
input_2 = '([{}])'
input_3 = '('
def is_valid(inp):
is_empty = False
stack = []
if len(inp)%2 == 0:
for i in range(0,len(inp)):
if(inp[i] == '(' or inp[i] == '[' or inp[i] == '{'):
stack.append(inp[i])
elif(inp[i] == ')' and not empty(stack) and stack[len(stack)-1] == '('):
stack.pop()
elif(inp[i] == ']' and not empty(stack) and stack[len(stack)-1] == '{'):
stack.pop()
elif(inp[i] == '}' and not empty(stack) and stack[len(stack)-1] == '{'):
stack.pop()
if(empty(stack)):
is_empty = empty(stack)
return(is_empty)
def empty(stack):
if stack == []:
return True
else:
return False
print(input_1,' -> ',is_valid(input_1))
print(input_2,' -> ',is_valid(input_2))
print(input_3,' -> ',is_valid(input_3))