题解 | #密码强度等级#

密码强度等级

http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

import sys


symbols = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'


def grade_pwd(s: str) -> str:
    # 对长度评分
    if len(s) <= 4:
        length_score = 5
    elif len(s) <=7:
        length_score = 10
    else:
        length_score = 25
    
    has_upper = has_lower = has_number = has_symbol = False
    number_score = symbol_score = 0
    for c in s:
        # 对数字进行评分
        if c.isdigit():
            if not has_number:
                has_number = True
                number_score = 10
            else:
                number_score = 20
        # 对字符进行评分
        elif c in symbols:
            if not has_symbol:
                has_symbol = True
                symbol_score = 10
            else:
                symbol_score = 25
        elif c.isupper():
            has_upper = True
        elif c.islower():
            has_lower = True
    
    # 对字母进行评分
    letter_score = 0
    if has_upper and has_lower:
        letter_score = 20
    elif has_upper or has_lower:
        letter_score = 10
        
    additional_score = 0  # 奖励分
    if has_number and letter_score:
        additional_score = 2
    if has_symbol and additional_score:
        additional_score = 3
    if letter_score == 20 and additional_score == 3:
        additional_score = 5
    
    score = length_score + letter_score + number_score + symbol_score + additional_score
    if score >= 90:
        return 'VERY_SECURE'
    if score >= 80:
        return 'SECURE'
    if score >= 70:
        return 'VERY_STRONG'
    if score >= 60:
        return 'STRONG'
    if score >= 50:
        return 'AVERAGE'
    if score >= 25:
        return 'WEAK'
    else:
        return 'VERY_WEAK'
        
        
for pwd in sys.stdin:
    print(grade_pwd(pwd))
全部评论

相关推荐

点赞 评论 收藏
分享
头像
昨天 14:28
长沙理工大学
刷算法真的是提升代码能力最快的方法吗?&nbsp;刷算法真的是提升代码能力最快的方法吗?
牛牛不会牛泪:看你想提升什么,代码能力太宽泛了,是想提升算法能力还是工程能力? 工程能力做项目找实习,算法也分数据结构算法题和深度学习之类算法
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务