-2

partial_alphabetic我正在尝试制作一个带有字符串的函数。

此函数应获取字符串中的每个字符并找到其第 i 个位置partial_alphabetic并获取已知字母(a、b、c 等)中的相应字符,如果该字符未知,则应打印“!” 在它的位置。

这是我的代码:

def cipher_attack(partial_alphabetic,ciphertext):
    var = ""
    for i in xrange(len(ciphertext)):
        letter = ciphertext[i]
        for j in xrange(len(partial_alphabetic)):
            if(partial_alphabetic[j] == letter):
                c = partial_alphabetic.index(letter)
                var += alphabet[c%26]
        var += "!"                    
    print var


cipher_attack("!wertyuiopasdfghjklzxcvbnm","rqr")

它会打印d!!d!,但应该打印d!d

4

4 回答 4

1

for找到匹配项时跳出循环,然后给循环一个else:套件。该套件仅在循环中断时执行:

def cipher_attack(partial_alphabetic, ciphertext):
    var = ""
    for letter in ciphertext:
        for i, pa in enumerate(partial_alphabetic):
            if pa == letter:
                var += alphabet[i % 26]
                break
        else:
            var += "!"                    
    return var

请注意,您可以直接ciphertext循环,无需先创建索引。然而,拥有索引有助于匹配,但另外创建索引更容易使用.partial_alphabetic alphabetenumerate()

演示:

>>> import string
>>> alphabet = string.ascii_letters
>>> def cipher_attack(partial_alphabetic, ciphertext):
...     var = ""
...     for letter in ciphertext:
...         for i, pa in enumerate(partial_alphabetic):
...             if pa == letter:
...                 var += alphabet[i % 26]
...                 break
...         else:
...             var += "!"                    
...     return var
... 
>>> cipher_attack("!wertyuiopasdfghjklzxcvbnm", "rqr")
'd!d'
于 2013-10-05T17:02:50.597 回答
0

或使用str.translate

import string
def cipher_attack(partial_alphabetic, ciphertext):
    missing = ''.join(set(ciphertext).difference(partial_alphabetic))
    table = string.maketrans(
        partial_alphabetic+missing,
        string.letters[:len(partial_alphabetic)]+'!'*len(missing))
    return (ciphertext.translate(table))

print(cipher_attack('!wertyuiopasdfghjklzxcvbnm', 'rqr'))

印刷

d!d

通常,它的str.translate执行速度比逐字符循环字符串的等效代码快。在您的示例中,ciphertext它很短,几乎没有关系,但对于较长的文本,它可能很重要。

使用循环解决方案cipher_attack2进行比较:

In [43]: %timeit cipher_attack('!wertyuiopasdfghjklzxcvbnm', 'rqr'*10000)
1000 loops, best of 3: 696 µs per loop

In [44]: %timeit cipher_attack2('!wertyuiopasdfghjklzxcvbnm', 'rqr'*10000)
10 loops, best of 3: 32 ms per loop

In [45]: 32000/696.
Out[45]: 45.97701149425287
于 2013-10-05T17:07:34.013 回答
0

利用:

def cipher_attack(partial_alphabetic,ciphertext):
    var = ""
    for c in chipertext:
        if c in partial_alphabetic: var+=c
        else: var+='!'
    return var

cipher_attack("!wertyuiopasdfghjklzxcvbnm","rqr")
于 2013-10-05T17:01:50.973 回答
0

这可以像这样改进

def cipher_attack(partial_alphabetic,ciphertext):
    var = ""
    for i in xrange(len(ciphertext)):
        if ciphertext[i] in partial_alphabetic:
            var += alphabet[partial_alphabetic.index(ciphertext[i]) % 26]
        else:
            var += "!"
    print var

cipher_attack("!wertyuiopasdfghjklzxcvbnm","rqr")

输出

d!d
于 2013-10-05T17:03:20.170 回答