프로그래머스/Lv. 1
43. [1차] 다트 게임
Seohyeong Lee
2024. 7. 16. 18:20
유형 : 구현
시간 : 20분
def solution(dartResult):
answer = []
drlist = list(dartResult)
#각 문자열의 list 생성
score = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
bonus = ['S', 'D', 'T']
option = ['*', '#']
#문자열의 문자 하나씩 순회
ansidx = -1
flag = False
for i in range(len(drlist)):
if flag == True:
flag = False
continue
elif drlist[i] in score:
if drlist[i] == '1':
if drlist[i+1] == '0':
answer.append(10)
ansidx += 1
flag = True
else:
answer.append(int(drlist[i]))
ansidx += 1
else:
answer.append(int(drlist[i]))
ansidx += 1
elif drlist[i] in bonus:
if drlist[i] == 'D':
answer[ansidx] = answer[ansidx] ** 2
elif drlist[i] == 'T':
answer[ansidx] = answer[ansidx] ** 3
elif drlist[i] in option:
if drlist[i] == '*':
answer[ansidx] *= 2
if ansidx > 0:
answer[ansidx-1] *= 2
if drlist[i] == '#':
answer[ansidx] = -answer[ansidx]
return sum(answer)
내 풀이는 좀 길다.. 최적화를 좀 해보자.
우선 list comprehension을 통해서 option을 좀 더 쉽게 처리하는 방법이 있었다.
10의 경우에도 다른 풀이에서는 10을 k 등 다른 알파벳으로 대체하는 방법을 사용하였다.