https://www.acmicpc.net/problem/11655
11655번: ROT13
첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.
www.acmicpc.net
나의 코드
import sys
input = sys.stdin.readline().rstrip()
big = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
small = list("abcdefghijklmnopqrstuvwxyz")
result = []
for i in input:
if i in big:
if big.index(i)+13 >= len(big):
idx = (big.index(i)+13) % 26
result.append(big[idx])
else:
idx = big.index(i)+13
result.append(big[idx])
elif i in small:
if small.index(i)+13 >= len(small):
idx = (small.index(i)+13) % 26
result.append(small[idx])
else:
idx = small.index(i)+13
result.append(small[idx])
else:
result.append(i)
for i in result:
print(i, end="")
다른 분들은 아스키코드를 사용해서 참고해서 써보았다.
import sys
input = sys.stdin.readline()
result = ""
for i in input:
if ("a" <= i) and (i <= "z"): # i.islower()
i = ord(i) + 13
if i > 122: # ord(z) = 122 = 97 + 25
i -= 26
result += chr(i)
elif ("A" <= i) and (i <= "Z"): # i.isuppper()
i = ord(i) + 13
if i > 90: # ord(Z) = 90 = 65 + 25
i -= 26
result += chr(i)
else:
result += i
print(result)
'공부 > 알고리즘' 카테고리의 다른 글
[알고리즘][백준] 11656번 접미사 배열 - 파이썬(python) 풀이 (0) | 2024.02.27 |
---|---|
[알고리즘][백준] 10824번 네 수 - 파이썬(python) 풀이 (0) | 2024.02.27 |
[알고리즘][백준] 10820번 문자열 분석 - 파이썬(python) 풀이 (1) | 2024.02.27 |
[알고리즘][백준] 10808번 알파벳 개수 - 파이썬(python) 풀이 (1) | 2024.02.26 |
[알고리즘][백준] 1918번 후위 표기식 - 파이썬(python) 풀이 (0) | 2024.02.26 |