-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path괄호변환.py
49 lines (46 loc) · 1.08 KB
/
괄호변환.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
def right(s):
stack = []
for i in s:
if i == "(":
stack.append(i)
else:
if len(stack) == 0:
return False
break
else:
stack.pop()
answer = (len(stack) == 0)
return answer
def flip(i):
answer = ''
for j in i :
if j == "(" :
answer += ")"
else :
answer += "("
return answer
def solution(p):
answer = ''
u = ''
v = ''
if right(p) == True :
answer = p
else :
rights = 0
lefts = 0
for i in range(0,len(p),1) :
if p[i] == "(" :
rights += 1
else :
lefts += 1
if rights == lefts :
u = p[0:i+1]
v = p[i+1:]
if right(u) == True :
answer += (u+solution(v))
return answer
else :
answer += "("+solution(v)+")"+flip(u[1:len(u)-1])
return answer
return answer
print(solution("()))((()"))