我有两个功能。第一个将构建一个编码器来对给定的文本进行凯撒密码:
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
第二个将编码器应用于给定的文本:
def applyCoder(text, coder):
cipherText=''
for l in text:
if l in coder:
l=coder[l]
cipherText+=l
return cipherText
问题的第 3 部分要求我构建一个包装器,但由于我是编码新手,所以我不知道如何编写使用这两个函数的包装器。
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.
"""