题解 | 密码强度等级

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

全部评论

相关推荐

八股刚起步,看了javaguide,小林coding,还有面渣,感觉面渣是体验最好的,请问只看面渣够用吗,有不完善的需要补吗?
码农索隆:先背最基础的知识,然后理解情景题,现在面试大多数喜欢问情景题,更考验面试者的基础和临场发挥情况
点赞 评论 收藏
分享
每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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