[프로그래머스] [1차] 다트 게임

yewon Lee·2023년 4월 16일
0

😎코딩테스트 연습>2018 KAKAO BLIND RECRUITMENT>[1차] 다트 게임


✏ 문제 풀이(오류)

def solution(dartResult):
    answer = 0
    bonus = {"S" : "**1", "D" : "**2", "T" : "**3"}
    dartList = []
    numList = []
    for i, d in enumerate(dartResult, start = 0):
        
        if d in bonus: #보너스 체크
             dartList.append(bonus[d])
        
        elif d == "*" and i-2 == 0: #스타상 첫번째일 때
            dartList.append("*2")         
    
        elif d == "*": #스타상 첫번째가 아닐 때
            dartList.append("*2")
            print(dartList)
            dartList.insert(i-2,"*2")
            print(dartList)
            
                
        elif d == "#": #아차상일 때
            dartList.append("*(-1)")
            
        elif  d.isdigit() and i != 0 and dartList[-1].isdigit() == False: #숫자이고 10이 아닐 때
            dartList.append("+")
            dartList.append(d)
                
        else:
            dartList.append(d)
        
    result = ''.join(map(str, dartList))
    
    print(dartList)
    
    return eval(result)
테스트케이스 18, 20 실패
parameters: "1S2D3T*", result: 74 실패

elif d == "*": #스타상 첫번째가 아닐 때
            dartList.append("*2")
            print(dartList)
            dartList.insert(i-2,"*2")
            print(dartList)

이때 스타상이 제곱 앞에 가면 계산이 달라지는데 제곱보다 앞으로 가는중..
근데 이게 앞으로 갈 때도 있고 아닐 때도 있어서 더 모르겠는....

👍 풀이 수정

스타상 추가를 할 때 dartList의 인덱스와 dartResult의 인덱스가 차이나는 걸 고려 안했음

def solution(dartResult):
    answer = 0
    bonus = {"S" : "**1", "D" : "**2", "T" : "**3"}
    dartList = []
    numList = []
    for i, d in enumerate(dartResult, start = 0):
        
        if d in bonus: #보너스 체크
             dartList.append(bonus[d])
        
        elif d == "*" and i-2 == 0: #스타상 첫번째일 때
            dartList.append("*2")
            print(i)
    
        elif d == "*": #스타상 첫번째가 아닐 때
            dartList.append("*2")
            print(dartList)
            dartList.insert(len(dartList)-4,"*2") #수정한 부분!!!
            print(dartList)
            print(i)
                            
        elif d == "#": #아차상일 때
            dartList.append("*(-1)")
            
        elif  d.isdigit() and i != 0 and dartList[-1].isdigit() == False: #숫자이고 10이 아닐 때
            dartList.append("+")
            dartList.append(d)
                
        else:
            dartList.append(d)
            print(d)
        
    result = ''.join(map(str, dartList))
    
    print(dartList)
    print(result)
    
    return eval(result)
profile
시작

0개의 댓글