Algorithm/백준 BOJ
[백준] [Python] #5598 카이사르 암호
은세라
2021. 9. 22. 02:11
https://www.acmicpc.net/problem/5598
5598번: 카이사르 암호
가이우스 율리우스 카이사르(Gaius Julius Caesar)는 고대 로마 군인이자 정치가였다. 카이사르는 비밀스럽게 편지를 쓸 때, 'A'를 'D로', 'B'를 'E'로, 'C'를 'F'로... 이런 식으로 알파벳 문자를 3개씩 건
www.acmicpc.net
알파벳을 input으로 받고 char형 -> 아스키코드로 바꾸면 간단할 것 같았는데 char로 변환하는 방법을 몰랐다
그래서 알파벳을 문자열로 생성해서 배열의 index로 해결함
import sys
sys.stdin = open('input.txt', 'r')
input = input()
arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
for i in range(len(input)):
for j in range(len(arr)):
if arr[j] == input[i:i+1]: print(arr[j-3], end="")
근데 암만 생각해도 저건 좀 아닌거같고...
풀고나서 찾아보니 ord()라는 함수가 있더라..
https://www.programiz.com/python-programming/methods/built-in/ord
Python ord()
Python ord() In this tutorial, we will learn about the Python ord() function with the help of examples. The ord() function returns an integer representing the Unicode character. Example character = 'P' # find unicode of P unicode_char = ord(character) prin
www.programiz.com
for ch in input:
update_ch = chr(ord(ch) - 3)
if ch == 'A' or ch == 'B' or ch == 'C':
update_ch = chr(ord(ch) + 23)
print(update_ch, end='')
이렇게 훨씬 간단하게 끝냄 : )