728x90
import string
def solution(msg):
answer = [];
alpha_dict = {char: index + 1 for index, char in enumerate(string.ascii_uppercase)}
print(alpha_dict)
index = 26;
while msg :
wc = ""
for i in range(len(msg)) :
wc = wc + msg[i];
if wc not in alpha_dict :
# 사전에 새로운 단어 추가
index += 1;
alpha_dict[wc] = index;
# answer에 추가하기
wc = wc[:-1]
answer.append(alpha_dict[wc]);
break;
msg = msg[len(wc):]
if not msg :
answer.append(alpha_dict[wc])
return answer
풀이 및 회고
먼저 알파벳 딕셔너리를 생성합니다. 반복문을 통해 msg 를 순회하면서 딕셔너리에 없는 문자열이 있으면 먼저 딕셔너리에 추가해줍니다. 그리고 이전 문자열을 answer에 추가해줍니다. 그 후 msg를 앞으로 이동시켜줍니다.
마지막 문자열이 사전에 있을때를 대비해 마지막 코드를 실행합니다.
728x90
'프로그래머스' 카테고리의 다른 글
프로그래머스 LV1 공원산책 (0) | 2024.06.19 |
---|---|
프로그래머스 LV2 모음사전 (0) | 2024.06.19 |
프로그래머스 LV2 전화번호 목록 (0) | 2024.06.15 |
프로그래머스 LV1 완주하지 못한 선수 (0) | 2024.06.02 |
프로그래머스 LV2 타겟넘버 (1) | 2024.06.02 |