我一直在做着簡單的編程挑戰,試圖學習和實踐。然而,我總是看起來效率低下。如果沒有使用內置代碼(比如編碼方法),我是否可以提高程序的效率(通常我的效率)?提高整體效率的任何提示?
import string
alph = string.ascii_lowercase
def encrypt(text):
encryption = ""
for character in text:
index = 0
shift = 0
for letter in alph:
if letter == character:
if index > 23:
shift = abs(26 - (index+3))
encryption += alph[shift]
break
shift = index + 3
encryption += alph[shift]
index += 1
return encryption
def decrypt(text):
decryption = ""
for character in text:
index = 0
shift = 0
for letter in alph:
if letter == character:
if index < 3:
shift = abs(26 - (index+3))
decryption += alph[shift]
break
shift = index - 3
decryption += alph[shift]
index += 1
return decryption
這是目前非常廣泛 - 它已經會更好,如果你問一些結構的效率。你雖然沒有爲你定義效率是多少(寫作時,運行時性能......),所以不可能知道正確的答案是什麼。 –