Search

둘 만의 암호

입출력 예시

s
skip
index
result
"aukks"
"wbqd"
5
"happy"

나의 코드

def solution(s, skip, index): lst = [chr(x) for x in range(97, 123)] # alphabet list lst = [x for x in lst if x not in skip] answer = [lst[(lst.index(x)+index)%len(lst)] for x in s] return ''.join(answer)
Python
복사

다른 풀이

from string import ascii_lowercase def solution(s, skip, index): result = '' a_to_z = set(ascii_lowercase) a_to_z -= set(skip) a_to_z = sorted(a_to_z) l = len(a_to_z) dic_alpha = {alpha:idx for idx, alpha in enumerate(a_to_z)} for i in s: result += a_to_z[(dic_alpha[i] + index) % l] return result
Python
복사

개선점 분석

ascii_lowercase 라는 module을 알았으면 더 쉬웠을 것.
dictionary를 사용해서 더 빠른 속도로 찾을 것.