用余数来帮助找到索引?

时间:2018-09-11 17:39:43

标签: python-3.x

谁能解释为什么以下代码中需要“%26”?不带“%26”的结果似乎相同。 代码源http://www.php.net/manual/fr/function.htmlspecialchars.php

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []

    uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    for eachLetter in realText:
        if eachLetter in uppercase:
            index = uppercase.index(eachLetter)
            crypting = (index + step) % 26
            cryptText.append(crypting)
            newLetter = uppercase[crypting]
            outText.append(newLetter)
        elif eachLetter in lowercase:
            index = lowercase.index(eachLetter)
            crypting = (index + step) % 26
            cryptText.append(crypting)
            newLetter = lowercase[crypting]
            outText.append(newLetter)
    return outText

code = caesar_encrypt('abc', 2)
print(code)

2 个答案:

答案 0 :(得分:1)

这是因为,它使用ROT样式的加密技术对信息进行了加密。它使用该余数来确定用哪个字母替换真实字母。

答案 1 :(得分:1)

如果原始字符+ %26的位置超出范围,则需要step来避免过度映射列表。如果您在最后一个字符之后着陆,则将从位置0重新开始。


您发布的代码在文本上使用了大量搜索,这需要花费一些时间。

最好“查找”一个字符pos,然后向其添加step,然后查找分配给该总和的字符。您不需要完整的大小写映射:可以测试输入字符并使用.upper()从小写字母创建大写字母。

# lowercase and uppercase ascii can be taken from constants out of string module
import string

# create a dict that comntains character -> info, only for lowercase
cryptDict = {ch:pos for pos,ch in  enumerate(string.ascii_lowercase)} 

# add the inverse mapping (pos ->character)    
for k,v in cryptDict.items():
    cryptDict[v] = k

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []

    for letter in realText:
        # get what pos this letter is at
        pos = cryptDict.get(letter,None) # gets None if character not in, f.e. 8

        if pos is not None:
            # you need % 26 here, in case your pos + step goes bigger then 26
            # f.e. z = 25,step = 2 => 27 , you do not have any character thats
            # mapped to 27,so you % 26 and use b which is mapped to 1
            crypt = cryptDict[(pos + step)%26]

            # fix casing if input was uppercase
            if letter.isupper():
                crypt = crypt.upper()

            outText.append(crypt)
        else:
            outText.append(letter) # do not forget unmapped values

    return outText

code = caesar_encrypt('abc', 2)
print(code) # ['c','d','e']

cryptDict如下所示:

{0: 'a',  1: 'b',  2: 'c',  3: 'd',  4: 'e',  5: 'f',  6: 'g',  7: 'h',  8: 'i', 
 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 
18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 

'a':  0, 'c':  2, 'b':  1, 'e':  4, 'd':  3, 'g':  6, 'f':  5, 'i':  8, 'h':  7, 
'k': 10, 'j':  9, 'm': 12, 'l': 11, 'o': 14, 'n': 13, 'q': 16, 'p': 15, 's': 18, 
'r': 17, 'u': 20, 't': 19, 'w': 22, 'v': 21, 'y': 24, 'x': 23, 'z': 25}

参考: -string.ascii_lowercase