我正在尝试使用递归二进制搜索来浏览文本文件,以根据字典文本文件查找和打印拼写错误的单词。程序会提示您打开一个文本文件,然后将其读入列表,每个项目都是一个单词(wordList)。如果单词在字典中找到并且拼写正确,我只想将其传递给它。我的代码如下:
from Tkinter import *
import tkFileDialog
with open("dictionary.txt") as f:
dictionary = f.read().splitlines()
dictionary.sort
fileToReadFrom = tkFileDialog.askopenfilename()
fin = open(fileToReadFrom, "r")
content = fin.read()
wordList = re.findall("[a-zA-Z]+", content)
def isWordinDictionary():
left = 0
right = len(dictionary)-1
for i in range(len(wordList)):
while right >= left:
middle = (left+right)//2
if dictionary[middle] == wordList[i]:
return True
elif dictionary[middle] < wordList[i]:
left = middle+1
elif dictionary[middle] > wordList[i]:
right = middle - 1
elif left > right:
print wordList[i]
isWordinDictionary()
程序将运行但导致python无法响应。有什么建议吗?
答案 0 :(得分:3)
isWordinDictionary()
永远不会返回的第一个原因:
对isWordinDictionary()
左侧的每次递归调用都会重置为0
,右侧会重置为len(dictionary)-1
。
换句话说,只有当您要查找的单词恰好位于索引(len(dictionary)-1)//2
时,才会返回。
isWordinDictionary()
永远不会返回的第二个原因:
您应该将for循环移出函数定义。每次递归调用,它都会从头开始搜索wordlist中的每个单词。
你想要这样的东西:
def isWordinDictionary(word, left = 0, right = len(dictionary)-1):
while right >= left:
middle = (left+right)//2
if dictionary[middle] == word:
return True
elif dictionary[middle] < word:
left = middle+1
elif dictionary[middle] > word:
right = middle - 1
elif left > right:
print word
return isWordinDictionary(word, left, right)
for word in wordList:
isWordinDictionary(word)