둘만의 암호(python)

김희준·2023년 5월 10일
0
post-thumbnail

def get_skip_alphalist(skip):
skip = list(skip)
arr = []
for i in range(len(skip)):
skip[i] = ord(skip[i])

for i in range(97,123):
    if i in skip:
        continue
    arr.append(chr(i))

return arr

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_alphalist(skip)

shift_s = "".join([
    shift_alphabet(alphabet, index, alphabet_list) for alphabet in list(s)
])

return shift_s

🎈문제 해결

chr() : 문자를 ASCII CODE로 변환
ord() : ASCII를 문자로 변환

각 chr(), ord()를 활용하여 skip에 해당되면 문자를 한번 더 더하여 스킵하고
나머지는 index만큼 shift하여 solution함수를 작성

profile
No passion No jun

0개의 댓글