[파이썬3 코딩테스트] 둘만의 암호

Sy Rhee·2023년 2월 4일
0

문제 링크

문제 설명


예를 들어 s = 'aukks', skip = 'wbqd', index = 5일 때, a에서 5만큼 뒤에 있는 알파벳은 f지만 b와 d는 skip에 포함되므로 세지 않으며, 이에 따라 a는 h로 변환된다. 알파벳이 z를 초과할 경우 다시 a부터 세게 되며, 이러한 규칙에 따라 'aukks'는 'happy'로 변환된다.

제한조건

나의 풀이

alphabet = 'abcdefghijklmnopqrstuvwxyz' # 알파벳 문자열 생성

def solution(s, skip, index):
    alpha_skipped = ''
    # skip에 있는 알파벳 걸러내기
    for char in alphabet:
        if char in list(skip):
            pass
        else:
            alpha_skipped += char
            
    # 나머지 알파벳 index만큼 뒤의 알파벳으로 바꾸기
    # z 다음 알파벳이 다시 a로 오기 때문에 나머지를 적용하였다.
    alpha_shift = [alpha_skipped[(i+index)%len(alpha_skipped)] for i in range(len(alpha_skipped))]
    
    # 바꾼 리스트의 값으로 정답 문자열 (결과) 도출하기
    answer = ''
    for i in range(len(s)):
        for j in range(len(alpha_skipped)):
            if s[i] == alpha_skipped[j]:
                answer += alpha_shift[j]

    return answer

(더 보면 좋을) 참조한 풀이

profile
hello

0개의 댓글