몸과 마음이 건전한 SW 개발자

프로그래머스 Lv. 1 시저 암호 Python 본문

알고리즘

프로그래머스 Lv. 1 시저 암호 Python

스위태니 2024. 1. 4. 00:01

문제 링크

https://school.programmers.co.kr/tryouts/72052/challenges

정답 코드

def solution(s, n):
    answer = ''
    # print(ord("a"), ord("z")) 97 122
    # print(ord("A"), ord("Z")) 65 90
    for a in s:
        if a == " ":
            answer += " "
        else:
            if a.isupper():
                location = (ord(a) - 65 + n) % 26 + 65
                answer += chr(location)
            else:
                location = (ord(a) - 97 + n) % 26 + 97
                answer += chr(location)
                
    return answer

풀이 방법

- ord와 chr 활용

느낀점

- 오랜만에 ord와 chr을 사용하려니 기억이 잘 나지 않았다.