
문자열의 형태인 s를 입력으로 받아 n칸 밀어 결과를 도출하는 문제다.
- 아스키코드를 활용하는
ord()와chr()를 사용
->ord(): 문자 -> 아스키 변환
->chr(): 아스키 -> 문자 변환
- 대문자/소문자 구분하여 알파벳 아스키를 벗어나는 경우 -26
a혹은A로 돌아오도록 -26 수행
def solution(s, n):
answer = ''
ascii_arr = []
for i in range(len(s)):
if s[i].isupper(): # 대문자인 경우
if ord(s[i])+n > 90:
ascii_arr.append(chr(ord(s[i])+n-26))
else:
ascii_arr.append(chr(ord(s[i])+n))
elif s[i].islower():
if ord(s[i])+n > 122:
ascii_arr.append(chr(ord(s[i])+n-26))
else:
ascii_arr.append(chr(ord(s[i])+n))
else:
ascii_arr.append(" ")
answer = answer.join(ascii_arr)
return answer