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.
defcaesarCipher(s,k):k=k%26#Normalizeshiftvaluetofallwithintherange[0,25]result=[]#Directlycreatetheresultlist# Iterate through the string and apply transformationsforcharins:ifchar.isalpha():#Checkifthecharacterisalphabetic# Calculate shifted character based on casebase=ord('A')ifchar.isupper()elseord('a')new_char=chr((ord(char)-base+k)%26+base)result.append(new_char)else:result.append(char)#Non-alphabeticcharactersremainunchangedreturn''.join(result)#Joinlistintofinalstring
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 →
Here is my solution in python3: