-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution3.py
More file actions
84 lines (72 loc) · 2.08 KB
/
solution3.py
File metadata and controls
84 lines (72 loc) · 2.08 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def is_valid(expr):
if (expr[0].isdigit() or expr[-1].isdigit() or
expr.count('{') > 1 or
expr.count('{') == 1 and expr[0] != '{'):
return False
match = [('(', ')'), ('[', ']'), ('{', '}')]
opening = ['(', '[', '{']
stack = []
i = 0
for char in expr:
if char in opening:
if (len(stack) > 0 and char in stack or '(' in stack or
char == '(' and len(stack) > 0 and
stack[-1] == '{'):
return False
else:
if len(stack) == 0 and i > 0:
return False
stack.append(char)
elif char.isdigit():
continue
else:
if len(stack) == 0:
return False
last_open = stack.pop()
if (last_open, char) not in match:
return False
i += 1
return len(stack) == 0
def parentheses(string):
if string == '':
return 0
else:
return int(string)
def parse(string, opening, closing):
s1 = ""
s2 = ""
b = False
for char in string:
if char.isdigit() and not b:
s1 += char
elif char == opening:
b = True
s2 += '*'
elif char == closing:
b = False
s1 += '*'
else:
s2 += char
s1 = s1.split('*')
s2 = s2.split('*')
return (s1, s2)
def square_brackets(string):
s1, s2 = parse(string, '(', ')')
sum1 = sum([parentheses(n) for n in s1])
sum2 = sum([parentheses(n) * 2 for n in s2])
return (sum1 + sum2)
def curly_brackets(string):
s1, s2 = parse(string, '[', ']')
sum1 = sum([parentheses(n) for n in s1])
sum2 = sum([square_brackets(n) * 2 for n in s2])
return (sum1 + sum2)
def calculate(expr):
if is_valid(expr):
if expr[0] == '(':
print (parentheses(expr[1:-1]))
elif expr[0] == '[':
print (square_brackets(expr[1:-1]))
elif expr[0] == '{':
print (curly_brackets(expr[1:-1]))
else:
print ('NO')