【单词城堡】给出来字母,要求我们组合成可能的单词--python3

(15) 2024-04-11 13:01:01

【单词城堡】给出来字母,要求我们组合成可能的单词--python3 (https://mushiming.com/)  第1张
游戏效果如图所示,给出了一个单词的所有字母,'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()

效果:
【单词城堡】给出来字母,要求我们组合成可能的单词--python3 (https://mushiming.com/)  第2张

THE END

发表回复