2018 KAKAO BLIND RECRUITMENT - 다트 게임 (Level 1)

 

https://programmers.co.kr/learn/courses/30/lessons/17682


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re

def solution(dartResult):
    records_str = dartResult
    records     = re.findall("([\d]+)([SDT])([*#]?)", records_str)

    bonus_table  = {'S': 1, 'D': 2, 'T': 3}
    option_table = {'*': 2, '#': -1, '': 1}

    rst = 0
    prev_option = None
    for score, bonus, option in reversed(records):
        score = int(score)
        score **= bonus_table[bonus]
        score  *= option_table[option]
        score   = score*option_table[prev_option] if prev_option == '*' else score
        rst += score
        prev_option = option
    return rst

Complexity

$O(|\text{records}|)$