I have two functions.
The first will build a coder to Caesar cipher a given text:
def buildCoder(shift):
lettersU=string.ascii_uppercase
lettersL=string.ascii_lowercase
dic={}
dic2={}
for i in range(0,len(lettersU and lettersL)):
dic[lettersU[i]]=lettersU[(i+shift)%len(lettersU)]
dic2[lettersL[i]]=lettersL[(i+shift)%len(lettersL)]
dic.update(dic2)
return dic
The second will apply the coder to a given text:
def applyCoder(text, coder):
cipherText=''
for l in text:
if l in coder:
l=coder[l]
cipherText+=l
return cipherText
Part 3 of the problem asks me to build a wrapper, but as I am new to coding I have no idea how to code a wrapper that uses these two functions.
def applyShift(text, shift):
"""
Given a text, returns a new text Caesar shifted by the given shift
offset. Lower case letters should remain lower case, upper case
letters should remain upper case, and all other punctuation should
stay as it is.
text: string to apply the shift to
shift: amount to shift the text (0 <= int < 26)
returns: text after being shifted by specified amount.
"""
Think of each of your functions as something that takes a certain kind of data and gives you a different kind.
buildCodertakes ashiftand gives you acoder.applyCodertakes sometext(a string to be coded) and acoderand gives you the coded string.Now, you want to write
applyShift, a function that takes ashiftand sometextand gives you the coded string.Where can you get the coded string? Only from
applyCoder. It needstextand acoder. We have thetextbecause it was given to us, but we also need acoder. So let’s get thecoderfrombuildCoderusing theshiftthat we were provided.Taken together, this will look like: