IT/coding study

[programmers] [3차] 압축 (python)

seyeonHello 2022. 1. 18. 23:05

2018 KAKAO BLIND RECRUITMENT

- queue와 list사용

from collections import deque
def solution(msg):
    answer = []
    dics = []
    que=deque(msg)
    dics=[chr(i) for i in range(ord('A'),ord('Z')+1)] #A:0,B:1,..
    tmp=''
    while len(que)>0:
        tmp=''
        for _ in range(0,len(que)):
            tmp+=que[0]
            if tmp in dics:
                que.popleft()
            else: #사전에 포함이 안될 때
                answer.append(dics.index(tmp[:-1])+1) #사전에 포함안되는 문자는 빼기
                dics.append(tmp)
                break
    answer.append(dics.index(tmp)+1)
    return answer