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.
if upper: # case it's uppercase
if numval > ord('Z'): # check if have to reset to 1
numval = numval - sub_const
else: # case it's lowercase
if numval > ord('z'):
numval = numval - sub_const
return chr(numval)
def caesarCipher(s, k):
# Write your code here
new_s = str()
for c in s:
if c.isalpha():
ch = shiftLetter(c, k)
else: #non alpha chars
ch = c
new_s += ch
return new_s
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 →
def shiftLetter(ch, shift): sub_const = ord('z') - ord('a') + 1 shift = shift % 26 numval = ord(ch)+shift
upper = ch.isupper()
def caesarCipher(s, k): # Write your code here new_s = str() for c in s: if c.isalpha(): ch = shiftLetter(c, k) else: #non alpha chars ch = c new_s += ch return new_s