在朋友的手机上看到一个游戏:4 Pics 1 Word
给定四幅图片,单词长度和备选字母,要求回答出对应的单词。游戏界面如下图所示:
(这道题的答案是car)
游戏挺好玩的,但是由于单词量太小,挫折感很强。于是决定写个程序来暴力枚举:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ######################################### 2 ## Crack the game "4 Pics 1 Word" 3 ## 4 ## Author: grepall@gmail.com 5 ######################################### 6 7 def isQualifyWord(word, length, candStr): 8 capWord = word.upper() 9 acceptAlpha = list(candStr) 10 if len(capWord) != length: 11 return False 12 for al in capWord: 13 if acceptAlpha.count(al) == 0: 14 return False 15 acceptAlpha.remove(al) 16 return True 17 18 wordList = ["C:\Users\xxx\Downloads\ispell-enwl-3.1.20\english.%d" % i for i in range(0, 4)] 19 for wFile in wordList: 20 xwordFile = file(wFile) 21 oneWord = xwordFile.readline() 22 oneWord = oneWord.strip() 23 while len(oneWord) != 0: 24 if isQualifyWord(oneWord, 6, "MMMUUCDEIGL"): 25 print oneWord 26 oneWord = xwordFile.readline() 27 oneWord = oneWord.strip()
这个程序运行的基础是常见英文单词文本库,从一个叫做Kevin Atkinson的热心网友处下载(sourceforge,需FQ)
不得不说,Python用于处理文本以及干这类脏活实在太方便了。