2018 KAKAO BLIND RECRUITMENT - 압축
aps
w 와 c 를 msg 를 슬라이싱해서 구현.
상황마다 msg 를 슬라이싱하는 시작점 s 와 얼마만큼 슬라이싱할 간격 L 을 사용
``` dic = {'A' : 1 ,,,} a([]): 결과로 출력할 list, wc 를 그 때마다 append가
s(0): msg 에서 w 로 입력받을 슬라이싱 인덱스 시작점. l(1): msg 에서 s부터 얼마만큼 더가서 슬라이싱할 것인지. w = msg[s : s + l] c = msg[s + l : s + l + 1]
s = 0 l = 1
while True:
w = msg[s : s + l] c = msg[s + l : s + l + 1] wc 가 사전에 있으면 l += 1 # 존재하는 wc 가 끝까지라면, wc 사전번호 a에 추가하고, a 반환하고 종료 wc 가 사전에 없으면 w 사전번호 a 에 추가 (출력용) wc 사전에 추가 s += l l = 1 # s == len(msg) - 1 이면 msg[s:s+l] 의 사전번호 a 에 추가하고, a 반환하고 종료.
```python
def solution(msg):
dic = dict()
lst = [chr(i) for i in range(ord('A'), ord('Z') + 1)]
for idx, char in enumerate(lst):
dic[char] = idx + 1
a = []
s = 0
l = 1
while True:
w = msg[s : s + l]
c = msg[s + l:s + l + 1]
if (w + c) in dic:
l += 1
if (s + l) >= len(msg):
a.append(dic[w + c])
return a
else:
a.append(dic[w])
dic[w + c] = len(dic) + 1
s += l
l = 1
if s == len(msg) - 1:
a.append(dic[msg[s: s + l]])
return a
Last updated
Was this helpful?