https://school.programmers.co.kr/learn/courses/30/lessons/155652
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
s에 담긴 스트링을 index만큼 shift하는데 skip에 담긴 글자는 넘어간다.
def solution(s, skip, index):
answer = ''
alpha = list('abcdefghijklmnopqrstuvwxyz')
alphadic = {v:i for i,v in enumerate(alpha)}
for ch in s:
idx = alphadic[ch]
cnt = 0
while cnt < index:
idx += 1
if idx > 25:
idx = 0
if alpha[idx] not in skip:
cnt += 1
answer += alpha[idx]
return answer
a="aukks"
b="wbqd"
c=5
a = "z"
b = "abcdefghij"
c = 20
solution(a,b,c)
검색할 리스트에서 이미 skip을 제거한후 검색할 리스트에서 index를 사용 shift 하였다.
def solution(s, skip, index):
alphas = [chr(a) for a in range(ord("a"), ord("z")+1) if chr(a) not in skip]
return "".join([alphas[(alphas.index(a) + index) % len(alphas)] for a in s])
이 코드를 보고 내 풀이를 좀더 다듬었다.
def solution(s, skip, index):
answer = ''
alpha = [ch for ch in 'abcdefghijklmnopqrstuvwxyz' if ch not in skip]
alphadic = {v:i for i,v in enumerate(alpha) }
for ch in s:
answer += alpha[(alphadic[ch] + index) % len(alpha)]
return answer
'코딩테스트' 카테고리의 다른 글
프로그래머스 석유시추 (0) | 2024.06.26 |
---|---|
프로그래머스 코테 181188 요격 시스템 Lv1 (0) | 2024.06.24 |
프로그래머스 외계어 사전 (0) | 2024.06.22 |
프로그래머스 코테 연습문제 공원 산책 176963 (0) | 2024.06.21 |
프로그래머스 코테 연습 [PCCP 기출문제] 1번 / 붕대 감기 (0) | 2024.06.21 |