我想要一个程序在文件中查找字谜。例如:
>>>anagram('words.txt', 'top')
top
pot
该文件将包含一长串没有空格的单词。
tapmatlamebrainfamelookcookkoolkoocnamemane
这是我当前的代码:
def anagrams(filename, word):
infile = open(filename, 'r')
if not word:
return ['']
ret = []
for i, d in enumerate(word):
perms = anagrams(word[:i] + word[i+1:])
for perm in perms:
ret.append(d + perm)
for i in ret:
if i in infile:
print (i)
else:
pass