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.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'caesarCipher' function below.## The function is expected to return a STRING.# The function accepts following parameters:# 1. STRING s# 2. INTEGER k#defcaesarCipher(s,k):# Write your code here# time is O(n)# incase k is larger than 26, we % so move is between 0 - 26move=k%26result=[]forletterins:ifletter.isupper():# ord() converts str to ACII num, and chr converts it back# - 65 so it goes from 0 - 26, % 26 so after adding move it's # still within 26 letter range, + 65 to go back to ACIIletter=chr((ord(letter)-65+move)%26+65)elifletter.islower():# lower letter a starts at 97letter=chr((ord(letter)-97+move)%26+97)result.append(letter)#combind letters in the arr result with "" (nothing) in betweenreturn"".join(result)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 →