파이썬 알고리즘 244번 | [프로그래머스 다트 게임]

Yunny.Log ·2022년 8월 18일
0

Algorithm

목록 보기
248/318
post-thumbnail

244. 2018 KAKAO BLIND RECRUITMENT 1차 다트 게임

1) 어떤 전략(알고리즘)으로 해결?

  • 문자열 처리

2) 코딩 설명

<내 풀이>


def solution(dartResult):
    dartResult = list(dartResult)
    startidx = 0
    divide_three = []
    # (1) 세개로 뽀개는 과정 
    for d in range(len(dartResult)) : 
            if (dartResult[d].isdigit() and not ((d>0 and dartResult[d-1]=='1') and dartResult[d]=='0')) : 
                endidx = d-1
                if endidx > 0 and len(dartResult[startidx:endidx+1])>1:
                    divide_three.append(dartResult[startidx:endidx+1]) 
                startidx = d
            
            elif d==len(dartResult)-1 : # 맨 마지막에 오면
                # if dartResult[-4].isdigit() : # 10 에다가 옵션 있는 경우
                #     divide_three.append(dartResult[len(dartResult)-4:len(dartResult)])
                # elif dartResult[-3].isdigit() :
                #     divide_three.append(dartResult[len(dartResult)-3:len(dartResult)])
                # else : # 뒤에서 두자리가 숫자지
                #     divide_three.append(dartResult[len(dartResult)-2:len(dartResult)])
                divide_three.append(dartResult[endidx+1:len(dartResult)])

    # print(divide_three)

    # (2) 점수 계산 과정 
    total = [] # 점수 (get) / S D T (sdt) / 보너스 (bonus)
    bonus = False # 보너스가 false 면 보너스 없는 것, false 아닐 때 연산 

    for dt in range(len(divide_three)) : 
        # print(total)
        if divide_three[dt][0]=='1' and divide_three[dt][1]=='0' :
            get = 10
            sdt = divide_three[dt][2]
            if len(divide_three)>3 :
                bonus = divide_three[3]
        else : 
            get = int(divide_three[dt][0])
            sdt = divide_three[dt][1]
            if len(divide_three[dt])>2 :
                bonus = divide_three[dt][2]
        # 2-1 ) sdt 따라서 제곱 처리 ~ 
        if sdt == 'S' :
            get**=1
        elif sdt == 'D' :
            get**=2
        else : 
            get**=3

        # 2-2 bonus 처리 
        if bonus!=False : 
            
            if bonus == "*" :
                if dt>0 : # 처음 아니라면 
                    total[dt-1]*=2
                    get*=2
                else : # 처음이라면
                    get*=2

            else : # 보너스가 샵 인 경우 
                get*=-1
        
        total.append(get)
    
        bonus = False # 보너스 다시 False 처리 

    # print("tot : " , total)
    answer = sum(total)
    return answer


print(solution("1D2S3T*"))

<반성 점>

  • 예외처리 해줄 게 많아서 피곤했다. 그러나 그럴수록 조건 잘 정리해서 한번에 딱 했음 되는건데 졸면서 해가지고 쓸데없이 오래걸렸다. 설계를 잘하고 임하자

<배운 점>

0개의 댓글