Baekjoon-2504-괄호의 값
20 Sep 2022stack = []
input_string = input()
def get_value(input_string):
for c in input_string:
if c == '(' or c == '[':
stack.append(c)
elif c == ')':
if len(stack) > 0:
if stack[-1] == '(':
stack.pop(-1)
stack.append(2)
elif len(stack) > 1 and isinstance(stack[-1], int) and stack[-2] == '(':
num = stack.pop(-1)
stack.pop(-1)
stack.append(2*num)
else:
return 0
else:
return 0
if c == ']':
if len(stack) > 0:
if stack[-1] == '[':
stack.pop(-1)
stack.append(3)
elif len(stack) > 1 and isinstance(stack[-1], int) and stack[-2] == '[':
num = stack.pop(-1)
stack.pop(-1)
stack.append(3*num)
else:
return 0
else:
return 0
if len(stack) > 1 and isinstance(stack[-1], int) and isinstance(stack[-2], int):
num = stack.pop(-1)
stack[-1] += num
if len(stack) == 1 and isinstance(stack[0], int):
return stack[0]
else:
return 0
print(get_value(input_string))
더 좋은 코드 발견
bracket = list(input())
stack = []
answer = 0
tmp = 1
for i in range(len(bracket)):
if bracket[i] == "(":
stack.append(bracket[i])
tmp *= 2
elif bracket[i] == "[":
stack.append(bracket[i])
tmp *= 3
elif bracket[i] == ")":
if not stack or stack[-1] == "[":
answer = 0
break
if bracket[i-1] == "(":
answer += tmp
stack.pop()
tmp //= 2
else:
if not stack or stack[-1] == "(":
answer = 0
break
if bracket[i-1] == "[":
answer += tmp
stack.pop()
tmp //= 3
if stack:
print(0)
else:
print(answer)