forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.py
More file actions
30 lines (26 loc) · 894 Bytes
/
caesar.py
File metadata and controls
30 lines (26 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/python
import sys
def caesarCipher(s, k):
# Works but fails a number of testcases
# Analysis - k = 100 fails, as the number always exceeds range of letters - fixed and 100 pass
string = list(s)
for i in range(len(string)):
if ord(string[i]) > 96 and ord(string[i]) < 123:
value = ord(string[i])+k
while value > 122:
sub = value - 122
value = 96+sub
string[i] = chr(value)
if ord(string[i]) > 64 and ord(string[i]) < 91:
value = ord(string[i])+k
while value > 90:
sub = value - 90
value = 64+sub
string[i] = chr(value)
return ''.join(string)
if __name__ == "__main__":
n = int(raw_input().strip())
s = raw_input().strip()
k = int(raw_input().strip())
result = caesarCipher(s, k)
print result