def main():
print("This program performs a spell-check in a file")
print("and prints a report of the possibly misspelled words.\n")
text = 'An old mann gathreed his abacus, and ran a mile. His abacus\n ran two miles!'
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
text = text.replace(ch, ' ')
words = text.lower().split(' ')
dic = ['a','abacus','an','and','arranged', 'gathered', 'his', 'man','mile','miles','old','ran','two']
#perform binary search for misspelled words
misw = []
for w in words:
m = binSearch(w,dic)
if m == -1:
misw.append(w)
print misw
打印为输出['mann', 'gathreed', '', '', 'abacus\n', '']
这些额外的空字符串''是标点符号的额外空格,您可以用空格代替它们。\n(换行符)有点问题,因为您可以在外部文本文件中看到它,但不能直观地解释它。您应该做的不是for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_``{|}~':只需检查每个字符.isalpha()请尝试以下操作:
^{pr2}$
输出:
This program performs a spell-check in a file
and prints a report of the possibly misspelled words.
['mann', 'gathreed']
你的二进制搜索工作完美!不过,你似乎没有删除所有的特殊字符。在
测试你的代码(用我自己的一句话):
打印为输出
['mann', 'gathreed', '', '', 'abacus\n', '']
这些额外的空字符串
^{pr2}$''
是标点符号的额外空格,您可以用空格代替它们。\n
(换行符)有点问题,因为您可以在外部文本文件中看到它,但不能直观地解释它。您应该做的不是for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_``{|}~':
只需检查每个字符.isalpha()
请尝试以下操作:输出:
希望这对你有帮助!如果你需要澄清或者有什么不起作用的话,请随时发表意见。在
相关问题 更多 >
编程相关推荐