-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdd2.py
More file actions
executable file
·47 lines (46 loc) · 1.39 KB
/
pdd2.py
File metadata and controls
executable file
·47 lines (46 loc) · 1.39 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
import re
input_str_for_test = ['{{[(2+3)*(1-3)]+4}*(14-3)}',
'{{[(2+3)*(1-3)]+4}*(14-3))}']
re_pattern = '[{}()\[\]]'
priority_map = {'{':3,'[':2,'(':1,')':-1,']':-2,'}':-3}
def is_valide_repr(input_str):
tokens = re.findall(re_pattern, input_str)
stack = []
priority = 0
for token in tokens:
priority = priority_map.get('token')
if token not in priority_map.keys():
continue
if token == '{':
if '[' in stack or '(' in stack:
return False
stack.append(token)
elif token == '[':
if '[' in stack or '(' in stack:
return False
stack.append(token)
elif token == '(':
if '(' in stack:
return False
stack.append(token)
elif token == '}':
if len(stack) and stack[-1] == '{':
stack.pop()
else:
return False
elif token == ']':
if len(stack) and stack[-1] == '[':
stack.pop()
else:
return False
elif token == ')':
if len(stack) and stack[-1] == '(':
stack.pop()
else:
return False
if priority > 1:
return False
return True
if __name__ == '__main__':
for s in input_str_for_test:
print is_valide_repr(s)