Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 프로그래머스 #sql #mysql #코딩테스트
- 카카오 코테
- 프로그래머스 #네트워크 #c++ #코딩테스트 #코테 #코테준비 #dfs
- 그리디알고리즘 #그리디 #백준 #우선순위큐 #최소힙 #최대힙 #알고리즘 #코딩테스트 #python
- 프로그래머스 #python #2021카카오 #카카오코테 #카카오인턴쉽
- 프로그래머스 #python #코딩테스트 #코테공부 #알고리즘 #dict
- 프로그래머스 #NULL 처리하기
- 프로그래머스 #c++ #코딩테스트
- 동
- 백준 #백준알고리즘 #알고리즘 #코딩테스트 #코딩테스트준비 #코테준비 #백준2110 #python #문제풀이
- 백준 #백준2217 #백준로프 #python
- 백준 #이거다시풀기
- 카카오 #프로그래머스 #python #코딩테스트 #오픈채팅방
Archives
- Today
- Total
say repository
[프로그래머스] [3차] 압축 python (*) 본문
728x90
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):
answer = []
dic = dict()
for i in range(ord('A'), ord('Z')+1):
dic[chr(i)] = i - ord('A') + 1
start, end = 0, 1
print(dic)
idx = 27 # dic 처음의 다음 인덱스
while end < len(msg)+1:
w = msg[start:end]
if w in dic:
end += 1
else:
answer.append(dic[w[:-1]])
dic[w] = idx # 인덱스 삽입
idx += 1
start = end -1
answer.append(dic[w])
return answer
풀이
영어 대문자 문자열로 조건에 맞게 비교하며 슬라이싱하는 문제이다.
슬라이싱 인덱싱이 조금 헷갈린다..
알파벳에 맞는 색인 문자를 같이 입력해야하니까
알파벳 사전을 dict() 으로 구현한다.
dic = dict()
for i in range(ord('A'), ord('Z')+1):
dic[chr(i)] = i - ord('A') + 1
이는 많이 나오니까 알아두자.
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 피로도 python (0) | 2022.05.26 |
---|---|
[프로그래머스] 소수 찾기 (level2) python (0) | 2022.05.25 |
[프로그래머스] JadenCase 문자열 만들기 python (0) | 2022.04.28 |
[프로그래머스] 여행경로 python (*) (0) | 2022.04.28 |
[프로그래머스] 구명보트 python (*) (0) | 2022.04.25 |