python 华为od 单词接龙

复制代码
sd=['word','dd','da','dc','dword','d']
# 计算出下一个接龙单词
def jl(sd,st):
    # sd.remove(st)
    sd = list(set(sd))
    sends = list(st)[-1]
    lg = []
    sd.sort()
    for i in sd:
        if i.startswith(sends):
            lg.append((i, len(i)))
    if lg==[]:
        return 0,0
    lg.sort(key=lambda x: x[1],reverse=True)
    maxlen = lg[0][1]
    maxlenl = []
    for i in lg:
        if maxlen <= i[1]:
            maxlenl.append(i)
    if len(maxlenl) != 1:
        maxlenl.sort(key=lambda k: k[0])
    sd.remove(maxlenl[0][0])
    return maxlenl[0][0],sd

res=[]
res.append(sd[4])
ld=len(sd)
st=sd[4]
sd.remove(sd[4])
for i in range(0,ld):
    st,sd=jl(sd,st)
    if st:
        res.append(st)
    else:
        break
print(res)
相关推荐
ZTLJQ7 小时前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
H5css�海秀7 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
阿贵---7 小时前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
无敌昊哥战神7 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
李昊哲小课9 小时前
第1章-PySide6 基础认知与环境配置
python·pyqt·pyside
2401_8942419210 小时前
用Pygame开发你的第一个小游戏
jvm·数据库·python
Zzzz_my11 小时前
正则表达式(RE)
pytorch·python·正则表达式
天天鸭11 小时前
前端仔写了个 AI Agent,才发现大模型只干了 10% 的活
前端·python·ai编程
setmoon21411 小时前
使用Scikit-learn构建你的第一个机器学习模型
jvm·数据库·python
2401_8331977312 小时前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python