-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparentheses_validator.py
More file actions
48 lines (41 loc) · 1.76 KB
/
parentheses_validator.py
File metadata and controls
48 lines (41 loc) · 1.76 KB
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
class ParenthesesValidator():
from typing import List
def __init__(self, content = '( init string)', fix = False):
self.content = content
self.fix = fix
print('>>', '\n', 'CONTENT : ',content)
def isValid(self,s: str) -> bool:
keys = [')', ']', '}']
values =['(', '[', '{']
parentheses = {key:value for key,value in zip(keys,values)}
stack_operations = []
for char in s:
if char in parentheses.values():
stack_operations.append(char)
elif (not stack_operations
or not stack_operations[-1] == parentheses.get(char)):
return False
elif stack_operations[-1] == parentheses.get(char):
stack_operations.pop(-1)
if not stack_operations:
return True
return False
def main(self):
if self.isValid(self.content):
print('\033[92m',self.isValid(self.content), '\033[0m\n........')
else:
print('\033[91m',self.isValid(self.content), '\033[0m\n........')
# TEST :
if __name__ == '__main__':
text = (
'(){}[]', # True
']', # False
'(])', # False
'{', # False
'(}', # False
'([{()(){[)[}{{}}{{{}}}]]}]})', # False
'(({[[[[[[{{{{{}[][()]}}}}]]]]]}]))', # False
'([{[([{()()}{[[[[{}{}{}][()()()]]]]}])]}])', # True
)
for string in text:
ParenthesesValidator(string, True).main()