๊ด€๋ฆฌ ๋ฉ”๋‰ด

Algo ์“ฐ์ž

[Programmers] ๋‹คํŠธ๊ฒŒ์ž„ ๋ณธ๋ฌธ

๐Ÿ’ฏ CodingTest/Programmers

[Programmers] ๋‹คํŠธ๊ฒŒ์ž„

S.Honey 2022. 4. 18. 09:37

โ–ถ ๋ฌธ์ œ : 

 

 

โ–ถ ์ฝ”๋“œ :

 

def solution(dartResult):
    numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    divided = []
    answer = []
    temp = ''
    
    for i in range(len(dartResult)):
        if dartResult[i] in numbers and i == 0:
            temp += dartResult[i]
        elif dartResult[i] in numbers and dartResult[i-1] not in numbers:
            divided.append(temp)
            temp = dartResult[i]
        elif i == len(dartResult) -1 :
            temp += dartResult[i]
            divided.append(temp)
        else:
            temp += dartResult[i]
    index = 0
    score = {i:0 for i in range(3)}

    for expr in divided:
        tempNum = ''
        for c in expr :
            if c in numbers: 
                tempNum += c
            else:
                if score[index] == 0:
                    score[index] = int(tempNum)
    
                if c == 'S':
                    continue
                elif c =='D':
                    score[index] **= 2
                elif c =='T':
                    score[index] **= 3
                elif c == '*':
                    score[index] *= 2
                    if index > 0:
                        score[index-1] *= 2                        
                elif c == '#':
                    score[index] *= -1
        index += 1

    answer = sum(list(score.values()))
    return answer

 

โ–ถ ๋ฌธ์ œ ํ•ด์„ค :

1. ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ์ ‘๊ทผํ•˜์˜€๋‹ค 

2. ์ดˆ๊ธฐ dartResult์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์—์„œ ๊ฐ๊ฐ์˜ ์Šคํ…Œ์ด์ง€๋ณ„๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆˆ๋‹ค.

3. ๋‚˜๋ˆˆ ๋ฌธ์ž์—ด๋“ค์— ๋Œ€ํ•œ ์ ์ˆ˜๋ฅผ score์— ์ €์žฅํ•˜๋ฉด์„œ ์ ์ˆ˜๊ณ„์‚ฐ์„ ํ•ด์ค€๋‹ค.  

 

 

โ–ถ ์ถ”๊ฐ€ ๋‚ด์šฉ : 

 

+ ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด์— ์žˆ๋Š” ์ •๊ทœํ‘œํ˜„์‹์„ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์„ ๊ผญ ์ฐธ๊ณ ํ•ด๋ณด์ž

+ ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉ๋ฒ•๋„ ๊ผญ ๊ณต๋ถ€ํ•ด๋ณด์ž.