프로그래머스 (Lv1) 둘만의 암호

업데이트:     Updated:

카테고리:

태그: , , ,

프로그래머스 Lv.1 “둘만의 암호”

  1. 02월 05일 풀이

예를 들어 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
    

Velog

다른 사람들의 풀이

이 문제를 풀 때 참조한 풀이


def get_skip_alphabet_list(skip):
    return [chr(alpha_num) for alpha_num in range(ord('a'), ord('z') + 1) if chr(alpha_num) not in skip]


def shift_alphabet(alphabet, index, skip_alphabet_list):
    skip_alpha_len = len(skip_alphabet_list)
    return skip_alphabet_list[(skip_alphabet_list.index(alphabet) + index) % skip_alpha_len]
    

def solution(s, skip, index):    
    alphabet_list = get_skip_alphabet_list(skip)
    
    shift_s = "".join([
        shift_alphabet(alphabet, index, alphabet_list) for alphabet in list(s)
    ])
    
    return shift_s
    


오류나 틀린 부분이 있을 경우 언제든지 댓글 혹은 메일로 지적해주시면 감사하겠습니다!

맨 위로 이동하기

Programmers 카테고리 내 다른 글 보러가기

댓글남기기