본문 바로가기
알고리즘/[python] 백준 BOJ

[Python] 백준 11655 ROT13

by Alan_Kim 2022. 12. 16.
728x90
반응형

https://www.acmicpc.net/problem/11655

 

11655번: ROT13

첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.

www.acmicpc.net

 

 

문제 해결

- 매우 쉬운 문제이다.

- 사실 리스트 하나 만들고 ['A', 'B', ....] 해놓은 다음 나누기를 해도 되지만 ord(), chr()를 써서 메모리도 깔끔하게 쓰고 함수로 깔끔하게 푸는 것이 베스트이다. (ord()와 chr()을 써볼 수 있는 기회)

 

CODE

import sys
input = sys.stdin.readline

S = str(input().rstrip())
ans = ''
for spell in S:
    if spell.isupper():
        num = ord(spell) - ord('A')
        real_num = (num+13)%26
        x= chr(ord('A')+real_num)
        ans += str(x)
    elif spell.islower():
        num = ord(spell) - ord('a')
        real_num = (num+13)%26
        x = chr(ord('a')+real_num)
        ans += str(x)
    else:
        ans += spell
print(ans)
728x90
반응형

댓글