繁体   English   中英

如何使这个Python代码更有效

[英]How can I make this Python code more efficient

我非常感谢您对我的第一个Python项目的反馈! :d

基本上我正在编写一个凯撒密码,如果你知道我的意思,我认为它非常“优化/高效”,这是因为我复制并粘贴了decrypt()方法的decrypt()方法,我唯一改变的是而不是更多地旋转数字,我更少旋转它们。 这就是我所说的:

       newPosition = (abc.find(letter) - key) % 26

^^ Instead of having a + (plus) I made it a - (minus) ^^

有没有办法可以在newPosition行调用encrypt()方法? 或者我所做的是正确的,它不需要修复(我非常怀疑)

**请记住,我从今天开始就没有太多的Python知识(如果有的话),所以不要用一些超级复杂的代码来吹嘘我的大脑。 谢谢!!! **

abc = 'abcdefghijklmnopqrstuvwxyz'

def main():
    message = input("Would you like to encrypt or decrypt a word?")
    if message.lower() == "encrypt":
        encrypt()
    elif message.lower() == "decrypt":
        decrypt()
    else:
        print("You must enter either 'encrypt' or 'decrypt'.")
        main()


def encrypt():
    message = input("Enter a message to encrypt: ")
    message = message.lower()
    key = int(input("What number would you like for your key value?"))
    cipherText = ""
    for letter in message:
        if letter in abc:
            newPosition = (abc.find(letter) + key) % 26
            cipherText += abc[newPosition]
        else:
            cipherText += letter
    print(cipherText)
    return cipherText

def decrypt():
    message = input("Enter a message to decrypt: ")
    message = message.lower()
    key = int(input("What number would you like for your key value?"))
    cipherText = ""
    for letter in message:
        if letter in abc:
            newPosition = (abc.find(letter) - key) % 26
            cipherText += abc[newPosition]
        else:
            cipherText += letter
    print(cipherText)
    return cipherText

main()

一般来说, str.find在性能方面表现不佳。 这是O(n)的复杂性,这并不可怕,但你很少需要它。 在这种情况下,你可以使用ord将每个字母转换为它的序数,然后减去ord('a')得到0-25而不是97-122。

这特别有用,因为您可以使用chr转换回而无需查找。

for letter in message:
    if letter in string.ascii_lowercase:  # same as "abcdef..z"
        new_position = ((ord(letter) - ord('a') + key) % 26) + ord('a')
        new_ch = chr(new_position)
        ciphertext += new_ch

另请注意,使用+=连接字符串的速度不如str.join快。

new_letters = [chr(((ord(letter) - ord('a') + key) % 26) + ord('a')) if letter in ascii_lowercase else letter for letter in message]
ciphertext = "".join(new_letters)

因为那个chr(((ord(letter) - ord('a') + key) % 26) + ord('a'))是如此丑陋,我会把它重构成一个函数。

def rotate(letter, key=0):
    c_pos = ord(letter) - ord('a')
    rotated = c_pos + key
    modded = rotated % 26
    final_pos = modded + ord('a')
    return chr(final_pos)

new_letters = [rotate(c, key) if c in string.ascii_lowercase else c for c in letters]
ciphertext = "".join(new_letters)

可维护性:如果将输入与结果分开,则编写可测试的优秀代码会更容易。 现在你必须对stdin做一些猴子修补来为你的任何函数编写单元测试,但是如果你将用户输入的请求转移到main和out各自的函数中,那就容易多了。

def main():
    message = input("What's the message to encrypt/decrypt? ")
    key = int(input("What number would you like for your key value? "))
    choice = input("Choose: encrypt or decrypt. ")
    if choice == "encrypt":
        result = encrypt(message, key)
    elif choice == "decrypt":
        result = decrypt(message, key)
    else:
        # something here about a bad user input.

事实上,当你通过翻转钥匙的标志来考虑Caesar Cipher是可逆的时,你可以简单地做到:

    if choice == "encrypt":
        result = encrypt(message, key)
    elif choice == "decrypt":
        result = encrypt(message, key * (-1))

而根本不写一个decrypt功能!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM