题解 | 密码强度等级

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))

全部评论

相关推荐

点赞 评论 收藏
分享
牛客464620405号:随便投,随便找,中国经过40多年的改革开放,人才缺口和职位空缺是巨大的,中国现在属于遍地黄金的年代,属于90后和00大机遇的时代
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务