将两个相似的函数泛化为一个
Generalizing two similar functions into one
所以下面是一些能够对句子进行编码和解码的 Python 代码。我不确定如何定义一个函数,该函数采用三个参数来概括或组合这两个函数:mapInToOutEncode 和 mapInToOutDecode 合二为一。
alphabet = " abcdefghijklmnopqrstuvwxyz"
keyString ="kjihgfedcba zyxwvutsrqponml"
def mapInToOutEncode(inChar):
index = 0
found = 0
for char in alphabet:
if(char == inChar):
found = index #remember it in a variable found
else:
index +=1
outChar = keyString[found]
return(outChar)
def mapInToOutDecode(inChar):
index = 0
found = 0
for char in alphabet:
if(char == inChar):
found = index #remember it in a variable found
else:
index +=1
outChar = keyString[found]
return(outChar)
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOutEncode(char)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOutDecode(char)
return(outString)
print( encode("this is a crazy bit of news to share we attack at dawn" ))
print( decode(encode("this is a crazy bit of news to share we attack at dawn")))
首先,请注意您的函数 mapInToOutEncode
和 mapInToOutDecode
是相同的。为了获得您想要的功能,mapInToOutDecode
应该是:
def mapInToOutDecode(inChar):
index = 0
found = 0
for char in keyString:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphabet[found]
return(outChar)
请注意,我还添加了单词 break
,因此您的代码一旦找到它就不会继续寻找 inchar
。
现在,要获得您想要的(mapInToOutEncode
和 mapInToOutDecode
的通用函数),您可以:
def mapInToOut(inChar, alphFrom, alphTo):
index = 0
found = 0
for char in alphFrom:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphTo[found]
return(outChar)
要编码每个字符a
,您将使用mapInToOut(a, alphabet, keyString)
。要 解码 每个字符 b
,您可以使用 mapInToOut(b, keyString, alphabet)
.
因此,encode
和 decode
函数将如下所示:
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, alphabet, keyString)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, keyString, alphabet)
return(outString)
这是一个 完整的 工作示例(即,这应该是您的全部代码):
alphabet = " abcdefghijklmnopqrstuvwxyz"
keyString ="kjihgfedcba zyxwvutsrqponml"
def mapInToOut(inChar, alphFrom, alphTo):
index = 0
found = 0
for char in alphFrom:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphTo[found]
return(outChar)
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, alphabet, keyString)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, keyString, alphabet)
return(outString)
print ("this is a crazy bit of news to share we attack at dawn")
encoded_msg = encode("this is a crazy bit of news to share we attack at dawn")
print encoded_msg
print decode(encoded_msg)
尝试使用字典来做你的encoding/decoding,它会大大简化你的代码:
>>> alphabet = " abcdefghijklmnopqrstuvwxyz"
>>> keyString ="kjihgfedcba zyxwvutsrqponml"
>>> encoder = dict(zip(alphabet, keyString))
>>> decoder = dict(zip(keyString, alphabet))
>>> a = 'this is a string'
>>> q = ''.join(encoder.get(i, i) for i in a)
>>> q
'rcbskbskjksrtbxd'
>>> ''.join(decoder.get(i, i) for i in q)
'this is a string'
所以下面是一些能够对句子进行编码和解码的 Python 代码。我不确定如何定义一个函数,该函数采用三个参数来概括或组合这两个函数:mapInToOutEncode 和 mapInToOutDecode 合二为一。
alphabet = " abcdefghijklmnopqrstuvwxyz"
keyString ="kjihgfedcba zyxwvutsrqponml"
def mapInToOutEncode(inChar):
index = 0
found = 0
for char in alphabet:
if(char == inChar):
found = index #remember it in a variable found
else:
index +=1
outChar = keyString[found]
return(outChar)
def mapInToOutDecode(inChar):
index = 0
found = 0
for char in alphabet:
if(char == inChar):
found = index #remember it in a variable found
else:
index +=1
outChar = keyString[found]
return(outChar)
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOutEncode(char)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOutDecode(char)
return(outString)
print( encode("this is a crazy bit of news to share we attack at dawn" ))
print( decode(encode("this is a crazy bit of news to share we attack at dawn")))
首先,请注意您的函数 mapInToOutEncode
和 mapInToOutDecode
是相同的。为了获得您想要的功能,mapInToOutDecode
应该是:
def mapInToOutDecode(inChar):
index = 0
found = 0
for char in keyString:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphabet[found]
return(outChar)
请注意,我还添加了单词 break
,因此您的代码一旦找到它就不会继续寻找 inchar
。
现在,要获得您想要的(mapInToOutEncode
和 mapInToOutDecode
的通用函数),您可以:
def mapInToOut(inChar, alphFrom, alphTo):
index = 0
found = 0
for char in alphFrom:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphTo[found]
return(outChar)
要编码每个字符a
,您将使用mapInToOut(a, alphabet, keyString)
。要 解码 每个字符 b
,您可以使用 mapInToOut(b, keyString, alphabet)
.
因此,encode
和 decode
函数将如下所示:
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, alphabet, keyString)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, keyString, alphabet)
return(outString)
这是一个 完整的 工作示例(即,这应该是您的全部代码):
alphabet = " abcdefghijklmnopqrstuvwxyz"
keyString ="kjihgfedcba zyxwvutsrqponml"
def mapInToOut(inChar, alphFrom, alphTo):
index = 0
found = 0
for char in alphFrom:
if(char == inChar):
found = index #remember it in a variable found
break
else:
index +=1
outChar = alphTo[found]
return(outChar)
def encode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, alphabet, keyString)
return(outString)
def decode(inString):
outString = ''
for char in inString:
outString += mapInToOut(char, keyString, alphabet)
return(outString)
print ("this is a crazy bit of news to share we attack at dawn")
encoded_msg = encode("this is a crazy bit of news to share we attack at dawn")
print encoded_msg
print decode(encoded_msg)
尝试使用字典来做你的encoding/decoding,它会大大简化你的代码:
>>> alphabet = " abcdefghijklmnopqrstuvwxyz"
>>> keyString ="kjihgfedcba zyxwvutsrqponml"
>>> encoder = dict(zip(alphabet, keyString))
>>> decoder = dict(zip(keyString, alphabet))
>>> a = 'this is a string'
>>> q = ''.join(encoder.get(i, i) for i in a)
>>> q
'rcbskbskjksrtbxd'
>>> ''.join(decoder.get(i, i) for i in q)
'this is a string'