https://school.programmers.co.kr/learn/courses/30/lessons/17682
2018 KAKAO BLIND RECRUITMENT
def solution(dartResult):
score=[]
for i in range(len(dartResult)):
if dartResult[i:i+2] == '10':
continue
elif dartResult[i-1:i+1] == '10':
score.append(10)
elif dartResult[i].isdigit():
score.append(int(dartResult[i]))
else:
if dartResult[i] in ['S', 'D', 'T']:
score[-1] = score[-1]**(['S', 'D', 'T'].index(dartResult[i])+1)
elif dartResult[i] == '*':
try:
score[-2] = score[-2] * 2
score[-1] = score[-1] * 2
except:
score[-1] = score[-1] * 2
elif dartResult[i] == '#':
score[-1] = score[-1] * (-1)
return sum(score)
되게 오래 걸렸다..
'10'을 처리하는 법이 생각이 안 나서 꾸역꾸역 수정해서 맞췄음
isdigit()은 숫자형으로 변경할 수 있는 값이면 True를 반환함
타인 코드 보고서 좀 더 깔끔하게 수정해봄
def solution(dartResult):
score=[]
dartResult = dartResult.replace('10', 'k')
for i in range(len(dartResult)):
if dartResult[i].isdigit():
score.append(int(dartResult[i]))
else:
if dartResult[i] == 'k':
score.append(10)
elif dartResult[i] in ['S', 'D', 'T']:
score[-1] = score[-1]**(['S', 'D', 'T'].index(dartResult[i])+1)
elif dartResult[i] == '*':
try:
score[-2] *= 2
score[-1] *= 2
except:
score[-1] *= 2
elif dartResult[i] == '#':
score[-1] *= -1
return sum(score)
'10' 처리하는 부분 바꾸고, else 부분 연산을 좀 더 직관적으로 했음
카카오 기출 어렵다..
import re
def solution(dartResult):
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
option = {'' : 1, '*' : 2, '#' : -1}
p = re.compile('(\d+)([SDT])([*#]?)')
dart = p.findall(dartResult)
for i in range(len(dart)):
if dart[i][2] == '*' and i > 0:
dart[i-1] *= 2
dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]
answer = sum(dart)
return answer
정규표현식은 사회악인 것 같다.