我有两个功能。 首先将建立一个用于编码凯撒密码给定文本:如何在两个函数的python中创建一个包装?
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.
"""
那么你尝试过什么? –