본문 바로가기
Problem-solving/프로그래머스

프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [3차] 압축 (Python3)

by taehee.kim.dev 2020. 7. 13.

 

https://programmers.co.kr/learn/courses/30/lessons/17684

 

코딩테스트 연습 - [3차] 압축

TOBEORNOTTOBEORTOBEORNOT [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]

programmers.co.kr

# [3차]압축

dictionary = [0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
              'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']


def solution(msg):
    answer = []

    checking_last_index = 0
    while True:
        str_to_check = msg[:checking_last_index + 1]
        if str_to_check in dictionary:
            if checking_last_index == len(msg) - 1:
                answer.append(dictionary.index(str_to_check))
                break
        else:
            answer.append(dictionary.index(str_to_check[:-1]))
            dictionary.append(str_to_check)
            msg = msg[checking_last_index:]
            checking_last_index = 0
            continue
        checking_last_index += 1

    return answer

댓글