haju__log
[python][백준/BOJ] 11655번 : ROT13 (반례 O) 본문
https://www.acmicpc.net/problem/11655
11655번: ROT13
첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.
www.acmicpc.net
✅ 틀린 코드
import sys
s=sys.stdin.readline().strip()
for i in range(len(s)):
if s[i]>='a' and s[i]<='z':
tmp=ord(s[i])+13
if tmp>ord('z'):
tmp=chr(tmp-26)
else:
tmp=chr(tmp)
print(tmp,end='')
elif s[i]>='A' and s[i]<='Z':
tmp=ord(s[i])+13
if tmp>ord('Z'):
tmp=chr(tmp-26)
else:
tmp=chr(tmp)
print(tmp,end='')
else:
print(s[i],end='')

✅ 반례 찾기
- 예제 출력과 랜덤으로 내가 직접 넣어 본 예제 모두 잘 나왔는데,, 오류가 왜 뜰까 고민하다가 !
- 첫 입력이 공백일 경우를 생각하지 않고 코드를 짠 것을 확인하였다. 😂😂😂
- ex> '(공백)(공백)abc' 을 입력했을 경우, '(공백)(공백)nop' 출력해야함 => 처음 문자열을 입력받을 때, strip()을 썼더니 문자열 왼쪽,오른쪽 공백 모두 제거해버렸다. => rstrip()을 쓰자!

✅ 최종 코드
import sys
s=sys.stdin.readline().rstrip()
for i in range(len(s)):
if s[i]>='a' and s[i]<='z':
tmp=ord(s[i])+13
if tmp>ord('z'):
tmp=chr(tmp-26)
else:
tmp=chr(tmp)
print(tmp,end='')
elif s[i]>='A' and s[i]<='Z':
tmp=ord(s[i])+13
if tmp>ord('Z'):
tmp=chr(tmp-26)
else:
tmp=chr(tmp)
print(tmp,end='')
else:
print(s[i],end='')