반응형
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


주어진 문제 요구대로 코딩하면 되는 문제.
현재 입력 + 다음 단어가 색인되어 있지 않을 경우
- "현재 입력"의 색인을 배열에 저장한다.
- "현재 입력 + 다음 단어" 조합을 새로 색인한다
- "현재 입력"에 다음 단어를 대입한다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(msg): | |
table = dict() | |
for idx, value in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ",1): | |
table[value] = idx | |
last_idx = idx | |
idx = 1 | |
answer = [] | |
letter = msg[0] | |
while idx < len(msg): | |
# 현재 입력 + 다음 글자 조합이 색인에 없는 경우 | |
if letter + msg[idx] not in table: | |
# 현재 입력을 answer 배열에 저장 | |
answer.append(table[letter]) | |
# 새 단어의 색인값 저장 | |
last_idx += 1 | |
table[letter + msg[idx]] = last_idx | |
# 현재 입력 = '다음 단어'로 저장 | |
letter = msg[idx] | |
# 다음 단어 | |
idx += 1 | |
continue | |
# 이미 색인되어 있는 단어의 경우, 다음 단어를 현재 입력에 추가 | |
letter += msg[idx] | |
idx += 1 | |
answer.append(table[letter]) | |
return answer |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 백준 1890. 점프 (0) | 2020.03.05 |
---|---|
[Python] 백준 14500. 테트로미노 (0) | 2020.03.04 |
[Python] 백준 11724. 연결 요소의 개수 (1) | 2020.03.02 |
[Python] 백준 17471. 게리맨더링 (0) | 2020.02.29 |
[Python] 백준 12100. 2048(Easy) (1) | 2020.02.27 |