[英]How to create a wrap in python of two functions?
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. 问题的第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.
"""
Think of each of your functions as something that takes a certain kind of data and gives you a different kind. 将您的每个函数都视为需要某种类型的数据并为您提供不同类型的某种东西。
buildCoder
takes a shift
and gives you a coder
. buildCoder
进行了shift
并为您提供了coder
。
applyCoder
takes some text
(a string to be coded) and a coder
and gives you the coded string. applyCoder
接受一些text
(要编码的字符串)和coder
然后为您提供编码后的字符串。
Now, you want to write applyShift
, a function that takes a shift
and some text
and gives you the coded string. 现在,您要编写
applyShift
,该函数需要一个shift
和一些text
并为您提供编码的字符串。
Where can you get the coded string? 在哪里可以找到编码字符串? Only from
applyCoder
. 仅来自
applyCoder
。 It needs text
and a coder
. 它需要
text
和coder
。 We have the text
because it was given to us, but we also need a coder
. 我们收到了
text
因为它是给我们的,但是我们还需要一个coder
。 So let's get the coder
from buildCoder
using the shift
that we were provided. 因此,让我们使用提供的
shift
从buildCoder
获取coder
。
Taken together, this will look like: 两者合计,如下所示:
def applyShift(text, shift):
# Get our coder using buildCoder and shift
coder = buildCoder(shift)
# Now get our coded string using coder and text
coded_text = applyCoder(text, coder)
# We have our coded text! Let's return it:
return coded_text
Forget about the term "wrapper". 忘记术语“包装器”。 Simply write another function that makes a call to the other two, and returns the result.
只需编写另一个调用其他两个函数并返回结果的函数 。 You already got the function signature (its name and its arguments) and a description of the desired result.
您已经获得了函数签名(其名称和参数)以及对所需结果的描述。 So in the function body do this:
因此,在函数主体中执行以下操作:
buildCoder()
with the shift
argument and store the result in a variable coder
shift
参数调用buildCoder()
并将结果存储在变量coder
applyCoder()
with the arguments text
and coder
which you just stored, and store the result in cipher_text
text
和coder
调用applyCoder()
,并将结果存储在cipher_text
cipher_text
cipher_text
To test that your function works, write some test code that runs your applyShift
with some sample data, for example: 要测试您的功能是否正常运行,请编写一些测试代码,并使用一些示例数据来运行
applyShift
,例如:
print applyShift('Loremp Ipsum.', 13)
def applyShift(text, shift):
return applyCoder(text, buildCoder(shift))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.