嵌套循环减慢程序。我怎样才能让它更快?
Nested loops slowing down program. How can I make it faster?
import re
file=input("What is the name of your file? ")
def words_from_file(filename):
try:
f = open(filename, "r")
words = re.split(r"[,.;:?\s]+", f.read())
f.close()
return [word for word in words if word]
except IOError:
print("Error opening %s for reading. Quitting" % (filename))
exit()
dictionary_file=words_from_file("big_word_list.txt")
newfile=words_from_file(file)
def dictionary_check(scores, dictionary_file, full_text):
count=0
for item in full_text:
if item in dictionary_file:
count+=1
scores.append(count)
def decoder(item,shiftval):
decoded = ""
for c in item:
c=c.upper()
if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = ord(c)
num += shiftval
if num > ord("Z"):
num=num-26
elif num < ord("A"):
num=num+26
decoded+=chr(num)
else:
decoded = decoded + c
return decoded
shiftval=0
scores=[]
while shiftval<=26:
full_text=[]
for item in newfile:
result=decoder(item,shiftval)
full_text.append(result)
shiftval+=1
print(full_text)
dictionary_check(scores, dictionary_file, full_text)
highest_so_far=0
for i in range(len(scores)):
if scores[i]>highest_so_far:
i=highest_so_far
i+=1
else:
i+=1
fully_decoded=""
for item in newfile:
test=decoder(item,highest_so_far)
fully_decoded+=test
print(fully_decoded)
大家好
我有这个任务,我必须编写一个解码移位密码的程序。现在它可以工作,但速度非常慢。我怀疑这可能是因为嵌套循环。我不太确定从这一点到哪里去。
代码的一些解释:程序读入一个加密文件,其中每个字母移动一定量(即移动 5,每个 A 现在都是 F。这将对每个字母完成).该程序还读取字典文件。只有 26 个可能的班次,因此对于每个班次,它都会解码文件。该程序将为每个可能的班次获取文件并将其与字典文件进行比较。与字典文件最相似的将被转载为最终解密文件。
谢谢大家!
https://drive.google.com/file/d/0B3bXyam-ubR2U2Z6dU1Ed3oxN1k/view?usp=sharing
^程序、字典、加密解密文件都有link
只需更改第 16 行:
dictionary_file=set(words_from_file("big_word_list.txt"))
所以if item in dictionary_file:
是在常量时间而不是线性时间执行的。该程序现在在 4 秒内运行,禁用打印语句,
并在 highest_so_far=i
中更改 i=highest_so_far
,并将字典大写。
import re
file=input("What is the name of your file? ")
def words_from_file(filename):
try:
f = open(filename, "r")
words = re.split(r"[,.;:?\s]+", f.read())
f.close()
return [word for word in words if word]
except IOError:
print("Error opening %s for reading. Quitting" % (filename))
exit()
dictionary_file=words_from_file("big_word_list.txt")
newfile=words_from_file(file)
def dictionary_check(scores, dictionary_file, full_text):
count=0
for item in full_text:
if item in dictionary_file:
count+=1
scores.append(count)
def decoder(item,shiftval):
decoded = ""
for c in item:
c=c.upper()
if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = ord(c)
num += shiftval
if num > ord("Z"):
num=num-26
elif num < ord("A"):
num=num+26
decoded+=chr(num)
else:
decoded = decoded + c
return decoded
shiftval=0
scores=[]
while shiftval<=26:
full_text=[]
for item in newfile:
result=decoder(item,shiftval)
full_text.append(result)
shiftval+=1
print(full_text)
dictionary_check(scores, dictionary_file, full_text)
highest_so_far=0
for i in range(len(scores)):
if scores[i]>highest_so_far:
i=highest_so_far
i+=1
else:
i+=1
fully_decoded=""
for item in newfile:
test=decoder(item,highest_so_far)
fully_decoded+=test
print(fully_decoded)
大家好
我有这个任务,我必须编写一个解码移位密码的程序。现在它可以工作,但速度非常慢。我怀疑这可能是因为嵌套循环。我不太确定从这一点到哪里去。
代码的一些解释:程序读入一个加密文件,其中每个字母移动一定量(即移动 5,每个 A 现在都是 F。这将对每个字母完成).该程序还读取字典文件。只有 26 个可能的班次,因此对于每个班次,它都会解码文件。该程序将为每个可能的班次获取文件并将其与字典文件进行比较。与字典文件最相似的将被转载为最终解密文件。
谢谢大家!
https://drive.google.com/file/d/0B3bXyam-ubR2U2Z6dU1Ed3oxN1k/view?usp=sharing
^程序、字典、加密解密文件都有link
只需更改第 16 行:
dictionary_file=set(words_from_file("big_word_list.txt"))
所以if item in dictionary_file:
是在常量时间而不是线性时间执行的。该程序现在在 4 秒内运行,禁用打印语句,
并在 highest_so_far=i
中更改 i=highest_so_far
,并将字典大写。