26 Wed
๋คํธ ๊ฒ์
'''
https://programmers.co.kr/learn/courses/30/lessons/17682
๋คํธ ๊ฒ์
0. ์ซ์, ๋ณด๋์ค, ์ต์
์ ๋ํ ์ ์ ๊ณ์ฐ
1. ์ ๊ท์ ์ฌ์ฉ
'''
import re
def solution(dartResult):
record = re.findall('[0-9]{1,2}[SDT][*#]|[0-9]{1,2}[SDT]', dartResult)
scores = []
for i in record:
a = int(''.join(re.findall('[0-9]', i)))
b = ''.join(re.findall('[SDT]', i))
c = ''.join(re.findall('[*#]', i))
if b == 'S':
score = a
elif b == 'D':
score = a ** 2
else:
score = a ** 3
if c == "*":
score *= 2
if scores:
scores[-1] *= 2
elif c == "#":
score = -score
scores.append(score)
return sum(scores)
'''
if๋ฌธ์ผ๋ก ํด๋น index์ ๊ฐ์ ๋น๊ตํ๊ธฐ ๋ณด๋ค๋, ๋ฏธ๋ฆฌ ๋์
๋๋ฆฌํ ํด์ key๊ฐ์ผ๋ก ๋ฐ๋ก ์ฐธ์กฐํ๊ธฐ
์ ๊ท์์ ์ข ๋ ๊น๋ํ๊ฒ ์ธ ๊ฒ
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
'''
Last updated
Was this helpful?