We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import math
import os
import random
import re
import sys
#
Complete the 'caesarCipher' function below.
#
The function is expected to return a STRING.
The function accepts following parameters:
1. STRING s
2. INTEGER k
#
def caesarCipher(s, k):
z = k % 26
test = ''
alpahabet = "abcdefghijklmnopqrstuvwxyz"
cipher_alphabet = alpahabet[z:] + alpahabet[:z]
for x in s:
if x.isalpha():
if x.isupper():
a = alpahabet.index(x.lower())
test = test + cipher_alphabet[a].upper()
else:
a = alpahabet.index(x)
test = test + cipher_alphabet[a]
else:
test = test + x
return test
if name == 'main':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
s = input()
k = int(input().strip())
result = caesarCipher(s, k)
fptr.write(result + '\n')
fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
!/bin/python3
import math import os import random import re import sys
#
Complete the 'caesarCipher' function below.
#
The function is expected to return a STRING.
The function accepts following parameters:
1. STRING s
2. INTEGER k
#
def caesarCipher(s, k): z = k % 26 test = '' alpahabet = "abcdefghijklmnopqrstuvwxyz" cipher_alphabet = alpahabet[z:] + alpahabet[:z] for x in s: if x.isalpha(): if x.isupper(): a = alpahabet.index(x.lower()) test = test + cipher_alphabet[a].upper() else: a = alpahabet.index(x) test = test + cipher_alphabet[a] else: test = test + x return test
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')