我用 Python 编写了一个 Anagram 求解程序。我想听听你的意见,我是否做得对。让我解释一下逻辑:
- 首先,用户提供他/她希望为其生成单个单词字谜的两个单词的输入(2 个字符串值)
- 将两者连接起来,并导出第三个值。
- 第三个值由 itertools.permutations 函数处理,其中单词的所有可能排列都导出为列表。
- 该列表使用从列表派生的字符串值进行格式化。
- 至此,我打开了一个单词列表,将用作字典来比较字符串值是否为实际单词。
- 逐行读取文件并将字符串值与行进行比较。
- 如果找到匹配项,则程序在屏幕上将输出打印为字典匹配
请告诉我我是否正确地处理它或者是否可以提出任何改进建议。任何反馈表示赞赏。我是 Python 新手。
这是代码:
#This program has been created to solve anagram puzzles
# All the imports go here
#import re
import itertools
import fileinput
def anaCore():
print 'This is a Handy-Dandy Anagram Solving Machine'
print 'First, we enter the first word....'
anaWordOnly = False
firstWord = raw_input('Please enter the first word > ')
print 'Thank you for entering %r as your first word' % firstWord
print 'Now we enter the second word....'
secondWord = raw_input('Please enter the second word > ')
print 'Thank you for entering %r as your second word' % secondWord
thirdWord = firstWord+secondWord
print thirdWord
mylist = itertools.permutations(thirdWord)
for a in mylist:
#print a
mystr = ''.join(a)
for line in fileinput.input("brit-a-z.txt"):
if mystr in line:
print 'Dictionary match found', mystr
#print mystr
anaCore()