华为机试题 最长的密码python代码

def find_longest_password(passwords):
    # 将密码本中的密码存储到集合中
    password_set = set(passwords)
    candidates = []

    # 遍历每个密码
    for password in sorted(passwords, key=len, reverse=True):
        # 检查当前密码是否满足条件
        for i in range(1, len(password) + 1):
            if password[:-i] not in password_set:
                print(password[:-i])
                break
            else:
            # 如果去掉末尾的任意一位字符后仍然在密码集中,添加到候选列表中
                candidates.append(password)
                print(f"else:{password}")
                break
            

    # 如果没有找到满足条件的密码,返回空字符串
    if not candidates:
        return ""

    # 如果有多个满足条件的密码,返回字典序最大的那个
    # 首先按长度降序排序,然后按字典序升序排序
    candidates.sort(key=lambda x: (-len(x), x))
    return candidates[0]

# 输入处理
passwords = input("请输入密码本,用空格分隔每个密码: ").split()
# 调用函数并打印结果
print(find_longest_password(passwords))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值