-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalanced_paranthesis.py
62 lines (51 loc) · 1.33 KB
/
balanced_paranthesis.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def is_balanced(value):
x = []
length = len(value)
if length == 1 or length % 2 == 1:
return False
for i in value:
if is_open(i):
x.append(i)
else:
# print(x)
if is_match(x[-1], i):
del x[-1]
else:
return False
return not x
def is_open(value):
if value in ('{', '[', '('):
return True
return False
def is_match(first, second):
if first == '{' and second == '}':
return True
elif first == '[' and second == ']':
return True
elif first == '(' and second == ')':
return True
return False
print("Expected: True")
print("Actuals : {}".format(is_balanced("{()[{}]}")))
print()
print("Expected: True")
print("Actuals : {}".format(is_balanced("{[]{()}}")))
print()
print("Expected: True")
print("Actuals : {}".format(is_balanced("{[({})]}")))
print()
print("Expected: False")
print("Actuals : {}".format(is_balanced("{[({})]")))
print()
print("Expected: False")
print("Actuals : {}".format(is_balanced("[({})]}")))
print()
print("Expected: False")
print("Actuals : {}".format(is_balanced("[")))
print()
print("Expected: False")
print("Actuals : {}".format(is_balanced("}")))
print()
print("Expected: False")
print("Actuals : {}".format(is_balanced("[{}{}(]")))
print()