Skip to content

Commit 804b796

Browse files
authored
Create caesar-cipher-1.py
1 parent 50a1a9d commit 804b796

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

caesar-cipher-1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://www.hackerrank.com/challenges/caesar-cipher-1
2+
# old logic, may be using DICT can improve the code.
3+
#!/bin/python3
4+
5+
import sys
6+
7+
8+
n = int(input().strip())
9+
s = input().strip()
10+
k = int(input().strip())
11+
s = list(s)
12+
k = k%26
13+
14+
for i in range(n):
15+
if s[i].isalpha():
16+
if s[i].islower():
17+
if ord(s[i])+k > ord('z'):
18+
s[i] = chr(ord('a')-1+k-ord('z')+ord(s[i]))
19+
else:
20+
s[i] = chr(ord(s[i])+k)
21+
else:
22+
if ord(s[i])+k > ord('Z'):
23+
s[i] = chr(ord('A')-1+k-ord('Z')+ord(s[i]))
24+
else:
25+
s[i] = chr(ord(s[i])+k)
26+
print(''.join(s))
27+
28+
29+
30+

0 commit comments

Comments
 (0)