yuns

[3차] 압축 파이썬 python3 본문

algorithms/programmers

[3차] 압축 파이썬 python3

yuuuun 2022. 5. 24. 22:16
반응형

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

def solution(msg):
    dic = {}
    for i in range(1, 27):
        dic[chr(64 + i)] = i
    
    idx = 27
    answer = []
    k = 0
    while k < len(msg):
        tmp = msg[k]
        k += 1
        while k < len(msg):
            tmp += msg[k]
            k += 1
            if tmp not in dic:
                dic[tmp] = idx
                idx += 1
                k -= 1
                break
        if k < len(msg):
            answer.append(dic[tmp[:-1]])
        else:
            answer.append(dic[tmp])
    return answer


print(solution('KAKAO'),	[11, 1, 27, 15])
print(solution('TOBEORNOTTOBEORTOBEORNOT'),	[20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34])
print(solution('ABABABABABABABAB'),	[1, 2, 27, 29, 28, 31, 30])
반응형

'algorithms > programmers' 카테고리의 다른 글

[Lv2] [카카오 인턴] 수식 최대화  (0) 2020.11.19
[Lv2] 주식 가격  (0) 2020.11.19
Comments