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?