将两个相似的函数概括为一个

时间:2015-01-07 04:11:23

标签: python string python-3.x decode encode

以下是一些能够对句子进行编码和解码的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")))

2 个答案:

答案 0 :(得分:0)

首先,请注意您的函数mapInToOutEncodemapInToOutDecode是相同的。为了获得您的目标,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

现在,为了得到你想要的东西(mapInToOutEncodemapInToOutDecode的广义函数)你可以拥有:

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)

因此,encodedecode函数将如下所示:

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)

答案 1 :(得分:0)

尝试使用字典进行编码/解码,这将大大简化您的代码:

>>> 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'