정답 코드 1
def solution(dartResult):
answer = 0
result = list(dartResult)
for i in range(len(result)):
if not result[i].isdigit():
result[i] = ' ' + result[i] + ' '
result = list("".join(result).split())
stack = []
for i in range(len(result)):
if result[i].isdigit():
stack.append(int(result[i]))
continue
if result[i] == 'D':
stack[-1] = pow(stack[-1], 2)
elif result[i] == 'T':
stack[-1] = pow(stack[-1], 3)
if i < len(result) - 1 and result[i + 1] == '*':
if len(stack) < 2:
stack[-1] *= 2
else:
stack[-1] *= 2
stack[-2] *= 2
elif i < len(result) - 1 and result[i + 1] == '#':
stack[-1] *= (-1)
answer = sum(stack)
return answer
정답 코드 2
def solution(dartResult):
answer = []
dartResult = dartResult.replace('10', 'k')
dart = list(dartResult)
dart = ['10' if i == 'k' else i for i in dart]
options = ['S', 'D', 'T', '*', '#']
for j in dart:
if j in options:
if j == 'D':
answer[-1] = pow(answer[-1], 2)
elif j == 'T':
answer[-1] = pow(answer[-1], 3)
elif j == '*':
answer[-1] *= 2
if len(answer) > 1:
answer[-2] *= 2
elif j == '#':
answer[-1] *= (-1)
else:
answer.append(int(j))
return sum(answer)