๊ธ€ ์ž‘์„ฑ์ž: ํƒ์‹œ ์šด์ „์‚ฌ
๋ฐ˜์‘ํ˜•


๐ŸŽฏ ๋‹คํŠธ ๊ฒŒ์ž„ ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ


๐Ÿ˜ƒ ๋‚˜์˜ ์ฝ”๋“œ

import re

def solution(dartResult):
    answer = 0
    p = re.compile("(\d+)([a-zA-Z])(\*|#)?")
    scores = p.findall(dartResult)
    result = []
    for idx, score in enumerate(scores):
        point = score[0]
        bonus = score[1]
        option = score[2]
        if bonus == 'S':
            bonus = 1
        elif bonus == 'D':
            bonus = 2
        elif bonus == 'T':
            bonus = 3
        if option == '*':
            if idx == 0:
                result.append(int(point)**bonus*2)
            else:
                result[-1] *= 2
                result.append(int(point)**bonus*2)
        elif option == '#':
            result.append(int(point)**bonus*-1)
        else: result.append(int(point)**bonus)
    
    return sum(result)

์ •๊ทœ ํ‘œํ˜„์‹(Regular Expression) Python์˜ ๋‚ด์žฅ Module์ธ re๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹คํŠธ ๊ฒŒ์ž„์€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ˆ˜ | ๋ณด๋„ˆ์Šค | [์˜ต์…˜]๋กœ ์ด๋ฃจ์–ด์ง„ ์‹œํ–‰๋“ค์ด ๋ฌธ์ž์—ด์ด๋ผ๋Š” ํ•œ ๋ฉ์ด๋กœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ด์šฉํ•ด ์ด๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ๋‚˜๋ˆ ์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์“ฐ์ธ ์ •๊ทœ ํ‘œํ˜„์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

p = re.compile("(\d+)([a-zA-Z])(\*|#)?")

(\d+)๋Š” 1๊ฐœ ์ด์ƒ์˜ ์ˆซ์ž์— ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค.
([a-zA-Z])๋Š” ์•ŒํŒŒ๋ฒณ ํ•˜๋‚˜์— ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค.
(\*|#)?์€ *ํ˜น์€(|, OR) #์— ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์˜ต์…˜ ์ฆ‰ ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ์—†์„ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, *์™€ #์€ ?๋ฅผ ๋ถ™์—ฌ์„œ ์ด๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
๊ฐ๊ฐ์€ ์†Œ๊ด„ํ˜ธ(parenthesis)๋กœ ๋ฌถ์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค.

scores = p.findall(dartResult)

๋”ฐ๋ผ์„œ ์ด ๊ฒฐ๊ณผ ๊ฐ’์ธ scores๋Š” [('1', 'S', ''), ('2', 'D', '*'), ('3', 'T', '')] ์ด๋Ÿฐ ํ˜•ํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ด์ œ ๊ฐ ์‹œํ–‰์ด ์ ์ ˆํ•˜๊ฒŒ ๋ถ„๋ฆฌ๋˜์—ˆ์œผ๋‹ˆ ๋‚จ์€ ๊ฑด ์ ์ˆ˜์˜ ๊ณ„์‚ฐ์ด ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค. ์ ์ˆ˜ ๊ณ„์‚ฐ์€ scores๋ฐฐ์—ด์˜ ์•ž๋ถ€ํ„ฐ ํ™•์ธํ•˜๋ฉด์„œ ๋ฌธ์ œ ์„ค๋ช…์— ๋‚˜์˜จ ์ ์ˆ˜ ๊ณ„์‚ฐ๋ฒ•์— ๋”ฐ๋ผ ์งœ๋ฉด ํฐ ์–ด๋ ค์›€์€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ ๊ฐ ์‹œํ–‰๋ณ„ ์ตœ์ข… ์ ์ˆ˜๋ฅผ result๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ  ๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” sum(result)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋งˆ๋ฌด๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

Written with StackEdit.


๋ฐ˜์‘ํ˜•

'Algorithm > Problem Solving' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๐Ÿ‘ฉโ€๐Ÿ’ผ ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง - [1์ฐจ] 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ / Python / KAKAO BLIND RECRUITMENT  (0) 2019.02.14
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๐Ÿ‘จโ€๐Ÿ’ป ์บ์‹œ - [1์ฐจ] 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ / Python  (0) 2019.02.13
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๐ŸŽฏ ๋‹คํŠธ ๊ฒŒ์ž„ - [1์ฐจ] 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ / Python  (1) 2019.02.12
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๐Ÿ—บ ๋น„๋ฐ€์ง€๋„ - [1์ฐจ] 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ / Python  (0) 2019.02.12
๐Ÿฅ 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ…Œ์ŠคํŠธ 3์ฐจ Python ํ’€์ด ๋ชจ์Œ / KAKAO BLIND RECRUITMENT  (0) 2019.02.09
๐Ÿฅ 2018 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ…Œ์ŠคํŠธ 1์ฐจ Python ํ’€์ด ๋ชจ์Œ / KAKAO BLIND RECRUITMENT  (0) 2019.02.09