Skip to content

Commit 35e7241

Browse files
committed
Add Caesar-Cipher-1
1 parent 327554e commit 35e7241

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

HackerRank/Caesar-Cipher-1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Ceasar Cipher 1
2+
# https://www.hackerrank.com/challenges/caesar-cipher-1/problem
3+
4+
def caesarCipher(s, k):
5+
alphabet = "abcdefghijklmnopqrstuvwxyz".upper()
6+
ans = ""
7+
for char in s:
8+
if char.upper() in alphabet:
9+
rotated = alphabet[(alphabet.index(char.upper())+k)%26]
10+
ans += rotated if char.isupper() else rotated.lower()
11+
else:
12+
ans += char
13+
return ans
14+
15+
# Testcases
16+
# "middle-Outz" 2 -> "okffng-Qwvb"
17+
# "Always-Look-on-the-Bright-Side-of-Life" 5 -> "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj"
18+
19+
# Status - Accepted

0 commit comments

Comments
 (0)