题解 | 密码强度等级

import string

def password_strength(password):
    score = 0

    # 密码长度得分
    if len(password) <= 4:
        score += 5
    elif 5 <= len(password) <= 7:
        score += 10
    elif len(password) >= 8:
        score += 25

    # 字母得分
    has_upper = any(char.isupper() for char in password)
    has_lower = any(char.islower() for char in password)
    if has_upper and has_lower:
        score += 20
    elif has_upper or has_lower:
        score += 10

    # 数字得分
    digit_count = sum(char.isdigit() for char in password)
    if digit_count == 1:
        score += 10
    elif digit_count > 1:
        score += 20

    # 符号得分
    symbol_count = sum(1 for char in password if char in "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
    if symbol_count == 1:
        score += 10
    elif symbol_count > 1:
        score += 25
    # 奖励得分
    if has_upper and has_lower and digit_count > 0 and symbol_count > 0:
        score += 5
    elif (has_upper or has_lower) and digit_count > 0 and symbol_count > 0:
        score += 3
    elif (has_upper or has_lower) and digit_count > 0:
        score += 2
    # 根据得分评定安全等级
    if score >= 90:
        return 'VERY_SECURE'
    elif score >= 80:
        return 'SECURE'
    elif score >= 70:
        return 'VERY_STRONG'
    elif score >= 60:
        return 'STRONG'
    elif score >= 50:
        return 'AVERAGE'
    elif score >= 25:
        return 'WEAK'
    elif score >= 0:
        return 'VERY_WEAK'
    else:
        return 'UNKNOWN'

# 读取输入
password = input().strip()

# 输出密码等级
print(password_strength(password))

全部评论

相关推荐

屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务