https://www.hackerrank.com/challenges/caesar-cipher-1
Caesar Cipher | HackerRank
Encrypt a string by rotating the alphabets by a fixed value in the string.
www.hackerrank.com
1
2
3
4
5
6
7
8
9
10
11
12
|
def caesarCipher(s, k):
# Write your code here
answer = ''
k = k% 26
for i in s:
if i.isalpha():
if ord(i)+k>122 or (ord(i)<=90 and ord(i)+k>90) :
i = chr(ord(i) + k-26)
else:
i = chr(ord(i) + k)
answer+=i
return(answer)
|
cs |
k가 26이 넘어가면 한바퀴를 더 도는거니까 26(알파벳 총 개수)로 나눠줌 : k = k% 26
IF K is over 26, it means round over alphabets, So We should divide 26(Alphabets)
문자가 대문자거나 소문자인데 더해서 아스키코드상에 대문자로 나올수도 있으므로 예외처리
Case when the letter is Large or is small but added letter in ascii can be Large should be handled
if ord(i)+k>122 or (ord(i)<=90 and ord(i)+k>90) :
반응형
'python-algorithm' 카테고리의 다른 글
백준 20546 🐜 기적의 매매법 🐜 (0) | 2022.11.12 |
---|---|
백준 2422 한윤정이 이탈리아에 가서 아이스크림을 사먹는데 (0) | 2022.11.10 |
백준 1969 DNA (0) | 2022.11.10 |
백준 11576 Base Conversion (0) | 2022.11.10 |
백준 18258 큐 2 (0) | 2022.11.09 |
댓글