코테 준비
[프로그래머스] 둘만의 암호 (Python)
박수련
2024. 3. 7. 12:49
문제 요약
https://school.programmers.co.kr/learn/courses/30/lessons/155652
문자열 s의 알파벳을 index만큼 뒤의 알파벳으로 바꿔준다.
skip에 포함된 알파벳은 건너뛴다.
풀이
문자열 s를 for문으로 돌려서 각 알파벳을 index만큼 뒤에 있는 알파벳으로 바꿔주었다.
한칸씩 뒤로 가면서 cnt += 1
을 해주었고, skip에 포함된 알파벳이라면 그냥 넘겨주었다.
z에서 뒤로 넘어가게되면 a로 갈 수 있도록 % 26
을 해서 인덱스를 설정해줬다.
코드
def solution(s, skip, index):
answer = ""
alphabet = "abcdefghijklmnopqrstuvwxyz"
for letter in s:
idx = alphabet.index(letter)
i, cnt = 1, 1
while cnt <= index:
if alphabet[(idx + i) % 26] in skip:
i += 1
continue
cnt += 1
i += 1
answer += alphabet[(idx + i - 1) % 26]
return answer