반응형

출처: https://programmers.co.kr/learn/challenges

 

 

 

 

 

 

** Javascript

 

 

function solution(dartResult) {
    const bo = dartResult.split(/\d/g).filter((item) => item.length);
    const s = dartResult.split(/[SDT]|\*|\#/g).filter((item) => item.length);
    let strg = [];
    for (let i = 0; i < 3; i++) {
        const [ bonus, option ] = bo[i].split("");
        let score = Number(s[i]) ** "_SDT".indexOf(bonus);
        strg.push(score);
        if (option === "*") {
            for (let j = i - 1; j < i + 1; j++) {
                strg[j] = strg[j] * 2;
            }
        }
        if (option === "#") {
            strg[i] = strg[i] * -1;
        }
    }
    return strg.reduce((acc, curr) => acc += curr, 0);
}

 

 

 

 

** Python

 

 

import re
def solution(dartResult):
    s = re.findall(r'\d+', dartResult)
    bo = [i for i in re.split(r'(\d+)', dartResult) if not i.isdigit()][1:]
    _sum = [0, 0, 0]
    for i in range(len(s)):
        _sum[i] = int(s[i])
        for j in bo[i]:
            if '_SDT'.find(j) > 0:
                _sum[i] = _sum[i] ** '_SDT'.find(j)
            elif j == "*":
                _sum[i] = _sum[i] * 2
                if i != 0:
                    _sum[i-1] = _sum[i-1] * 2
            else:
                _sum[i] = _sum[i] * -1
    return sum(_sum)

 

 

 

반응형

+ Recent posts