Python Flip Cipher

时间:2013-01-22 22:27:44

标签: python

对于一个项目,我们必须创建一个程序,其中一个输入一个关键字和一个字符串,程序对其进行编码。例如

字符串:但我没想到。 关键词:仙境

该计划将“仙境”分成其在字母表中的位置:

WONDERLAND = 23,15,14,4,5,18,12,1,14,4

然后它会将位置1中的角色与位置23中的角色,位置2中的角色以及位置15中的角色等交换。

'但我不认为'会出现''nu n。ithdtBiItdk“

我已经能够成功编码字符串,但不能对其进行解码。这是我的代码:

def encode(keyword,t):
    key = []
    text = []

    for i in range(len(t)):
        text.append(t[i])

    for i in range(len(keyword)):
        key.append(ord(keyword[i].lower()) - 96)

    keynum = 0
    actnum = 0

    for i in range(len(text)):
        print str(i) + " " + str(text)

        if keynum > len(keyword) - 1:
            keynum = 0

        actnum = key[keynum]

        while (actnum + i) > len(text) - 1:
            actnum = actnum - len(text)

        temp = text[i]
        text[i] = text[i+actnum]
        text[i+actnum] = temp
        keynum += 1

    merged = ""

    for i in range(len(text)):
        merged += text[i]

    return merged

def decode(keyword,t):
    key = []
    text = []

    for i in range(len(t)):
        text.append(t[i])

    for i in range(len(keyword)):
        key.append(ord(keyword[i].lower()) - 96)

    keynum = 0
    actnum = 0
    total = len(keyword)

    for i in reversed(range(len(text))):
        print str(i) + " " + str(text)

        keynum = total

        while keynum > len(keyword) - 1:
            keynum -= len(keyword)
            keynum += 1

        if keynum < 0:
            keynum = len(keyword) - 1

        actnum = key[keynum]

        while (actnum + i) > len(text) - 1:
            actnum = actnum - len(text)

        temp = text[i]
        text[i] = text[i+actnum]
        text[i+actnum] = temp
        total -= 1

    merged = ""

    for i in range(len(text)):
        merged += text[i]

    return merged

x = encode("wonderland","But I didn't think.")
print decode("wonderland",x)

编辑:设置程序,以便如果指定的字符不存在,它将获取它应该是的字符并从中减去字符串的长度,直到它在里面字符串。

编辑:基本上,如果您可以帮我扭转'编码'功能,那就太棒了。

1 个答案:

答案 0 :(得分:0)

为了清晰起见,我稍微重构了一下代码。我认为问题是使用当前位置的偏移量:text[i+actnum]

def flip(keyword,t,unflip=False):
    text = list(t)
    key = map(lambda x: ord(x)-ord('a'), keyword.lower())

    trange = reversed(range(len(text))) if unflip else range(len(text))

    for i in trange:
        keynum = i%len(keyword)
        actnum = key[keynum]%len(text)
        text[i],text[actnum] = text[actnum],text[i]
        print '%2d <-> %2d  '%(i,actnum),''.join(text)

    return ''.join(text)


x = flip("WONDERLAND","But I didn't think.")
print
flip("WONDERLAND",x,unflip=True)

输出:

 0 <->  3    utBI didn't think.
 1 <-> 14    htBI didn't tuink.
 2 <-> 13    htBI didn't tuink.
 3 <->  3    htBI didn't tuink.
 4 <->  4    htBI didn't tuink.
 5 <-> 17    htBIkdidn't tuin .
 6 <-> 11    htBIktidn'd tuin .
 7 <->  0   ihtBIkt dn'd tuin .
 8 <-> 13   ihtBIkt tn'd duin .
 9 <->  3   ihtnIkt tB'd duin .
10 <->  3   iht'Ikt tBnd duin .
11 <-> 14   iht'Ikt tBnu ddin .
12 <-> 13   iht'Ikt tBnud din .
13 <->  3   iht Ikt tBnud'din .
14 <->  4   iht dkt tBnud'Iin .
15 <-> 17   iht dkt tBnud'I ni.
16 <-> 11   iht dkt tBnnd'I ui.
17 <->  0   iht dkt tBnnd'I ui.
18 <-> 13   iht dkt tBnnd.I ui'

18 <-> 13   iht dkt tBnnd'I ui.
17 <->  0   iht dkt tBnnd'I ui.
16 <-> 11   iht dkt tBnud'I ni.
15 <-> 17   iht dkt tBnud'Iin .
14 <->  4   iht Ikt tBnud'din .
13 <->  3   iht'Ikt tBnud din .
12 <-> 13   iht'Ikt tBnu ddin .
11 <-> 14   iht'Ikt tBnd duin .
10 <->  3   ihtnIkt tB'd duin .
 9 <->  3   ihtBIkt tn'd duin .
 8 <-> 13   ihtBIkt dn'd tuin .
 7 <->  0    htBIktidn'd tuin .
 6 <-> 11    htBIkdidn't tuin .
 5 <-> 17    htBI didn't tuink.
 4 <->  4    htBI didn't tuink.
 3 <->  3    htBI didn't tuink.
 2 <-> 13    htBI didn't tuink.
 1 <-> 14    utBI didn't think.
 0 <->  3   But I didn't think.