Covenant


Kakao: vivi의 카카오 겨울 개발자 인턴십 성장기를 읽어보세요~


📖 다트게임 문제 확인




✏️ 문제 해결방법


  • 카카오 코테 == 문자열 처리 를 알리는 문제가 아닌가 싶습니다. 정확한 문자열 처리와 놓친 조건이 없는지 주의깊게 봐야합니다.
    • 본 문제에서 0~10 까지의 숫자가 들어옵니다. 단어를 한 글자씩 읽어서 숫자인지 처리한다면 10이 들어왔을 때 문제가 발생합니다.
  • 카카오 코테에서 앞 번호 + 문제의 길이가 길다 => 쉬운문제, 나는 이 문제를 무조건 풀 수 있다. 라고 생각하시면 됩니다.
  • 조건 요약
    • 1번: 숫자 + [S, D, T] => 숫자^1, 숫자 ^2, 숫자^3
    • 2번: *의 경우 바로 이전 점수와 현재 점수의 2배. / 맨 앞에 오는 경우 현재 점수만 2배
    • 3번: #의 경우 해당 점수를 음수로 변환
  • 사실 조건 요약을 하면 간단합니다. 코테 특유의 압박 속에서 조건을 남김없이 읽어야 점수를 획득 할 수 있을 것입니다.


⭕ 최종 풀이 1

def solution(dartResult):
    total = 0
    i = 0
    tmp = 0
    prev = -1
    while i < len(dartResult):
        if dartResult[i].isdigit():
            prev = tmp
            total += tmp
            if i+1 < len(dartResult) and dartResult[i+1] == "0":
                tmp = 10
                i += 1
            else:
                tmp = int(dartResult[i])
        elif dartResult[i] == "S":
            tmp *= 1
        elif dartResult[i] == "D":
            tmp = pow(tmp, 2)
        elif dartResult[i] == "T":
            tmp = pow(tmp, 3)
        elif dartResult[i] == "*":
            total = total + prev
            tmp *= 2
        elif dartResult[i] == "#":
            tmp *= -1
        i += 1

    total += tmp
    return total

  • 10의 숫자가 들어왔는지 확인하는 코드입니다.
if dartResult[i].isdigit():

    if i+1 < len(dartResult) and dartResult[i+1] == "0":
        tmp = 10
        i += 1
  • 숫자가 나오면 다음 글자도 읽어서 숫자가 등장하는지 검사합니다.
  • 그러나 위의 방법은 불편하기에 최종 풀이 2와 같이 개선할 수 있습니다.


⭕ 최종 풀이 2

def solution(dartResult):
    total = 0
    i = 0
    tmp = 0
    prev = -1
    dartResult = dartResult.replace("10", "K")
    while i < len(dartResult):
        if dartResult[i].isdigit() or dartResult[i] == "K":
            prev = tmp
            total += tmp
            if dartResult[i] == "K":
                tmp = 10
            else:
                tmp = int(dartResult[i])
        elif dartResult[i] == "S":
            tmp *= 1
        elif dartResult[i] == "D":
            tmp = pow(tmp, 2)
        elif dartResult[i] == "T":
            tmp = pow(tmp, 3)
        elif dartResult[i] == "*":
            total = total + prev
            tmp *= 2
        elif dartResult[i] == "#":
            tmp *= -1
        i += 1

    return total + tmp
  • dartResult = dartResult.replace("10", "K") 를 통해서 10의 문자를 K로 변환하였습니다.
  • 숫자 혹은 K가 문자열에 포함되어있으면 숫자로 변환하면 됩니다.