遊戲效果如圖所示,給出了一個單詞的所有字母,'atstle’要求組成正確的單詞才能開鎖,下面就用代碼枚舉出所有在有道詞典有正確釋義的單詞。
import requests
# 通過有道詞典查詢單詞是否有效
def searchWord(word):
youdaourl = ('http://dict.youdao.com/suggest/minisuggest?q=%s')%(word)
req = requests.get(youdaourl)
wordTag = ('<![CDATA[%s]]>')%(word)
if req.text.find(wordTag) != -1:
return True
else:
return False
#得到字符的全排列
def combination(s=''):
if len(s)<=1:
return [s]
sl=[]
for i in range(len(s)):
for j in combination(s[0:i]+s[i+1:]):
sl.append(s[i]+j)
return set(sl)
def inputword():
target = input('請輸入單詞❓:')
print('\033[5;31mloading...\033[0m')
list = combination(target)
arr = []
for word in list:
if searchWord(word):
arr.append(word)
print('\033[32mload end\033[0m')
print('\033[0;33m可能組成的單詞:\033[0m', arr)
inputword()
效果: