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 | 31 |
Tags
- 운영체제
- level3
- DP
- 가상메모리
- 딕셔너리
- 재귀
- BFS
- 백준
- 다이나믹 프로그래밍
- 프로그래머스
- level0
- dfs
- 스택
- MYSQL
- programmers
- level1
- 구현
- level2
- 가상메모리 관리
- 힙
- 수학
- 파이썬
- BOJ
- 그리디
- 다익스트라
- dict
- 코딩테스트
- 브루트포스
- python
- N과M
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level2 [압축] 파이썬 본문
https://programmers.co.kr/learn/courses/30/lessons/17684
#프로그래머스 압축
def solution(msg):
msg=list(map(str,msg))
answer = []
dic={'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8,'I':9,'J':10,'K':11,'L':12,'M':13,'N':14,'O':15,'P':16,'Q':17,'R':18,'S':19,'T':20,'U':21,'V':22,'W':23,'X':24,'Y':25,'Z':26}
i=0
while i<len(msg):
s=''
s+=msg[i]
while i<(len(msg)-1) and s in dic:
i+=1
s+=msg[i]
if len(s)==1:
answer.append(dic[s])
i+=1
elif s in dic:
answer.append(dic[s])
i+=1
else:
answer.append(dic[s[:-1]])
dic[s]=len(dic)+1
return answer
dic의 초기값을 세팅하고, s에 값을 삽입한다. 길이가 1이면, answer에 index값을 삽입하고, i가 msg의 마지막 index이면 (if문에서 s가 dic에 속하면) answer에 index값을 삽입한다. 그 경우가 아니라면 s를 dic의 값을 추가하고, dic에 있는 index값을 answer에 삽입한다.
더 좋은 코드가 있을 거 같은데 잘 못풀었다...
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level2 [이진 변환 반복하기] 파이썬 (0) | 2021.12.06 |
---|---|
프로그래머스 level2 [점프와 순간 이동] 파이썬 (0) | 2021.12.06 |
프로그래머스 level2 [멀쩡한 사각형] 파이썬 (0) | 2021.12.02 |
프로그래머스 level3 [추석 트래픽] 파이썬 (0) | 2021.12.02 |
프로그래머스 level2 [다음 큰 숫자] 파이썬 (0) | 2021.12.02 |