题解 | #HJ20 密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

import sys


lower_letter = "".join([chr(ord("a") + i) for i in range(26)])
upper_letter = "".join([chr(ord("A") + i) for i in range(26)])
num_string = "0123456789"


def verify_pwd(pwd):
    pwd = pwd.strip()
    # 校验规则1
    if len(pwd) < 8:
        return "NG"

    # 校验规则2
    count = 0
    flag_1 = flag_2 = flag_3 = flag_4 = False
    for letter in pwd:
        if not flag_1 and letter in lower_letter:
            count += 1
            flag_1 = True

        if not flag_2 and letter in upper_letter:
            count += 1
            flag_2 = True

        if not flag_3 and letter in num_string:
            count += 1
            flag_3 = True

        if (
            not flag_4
            and letter not in lower_letter
            and letter not in upper_letter
            and letter not in num_string
        ):
            count += 1
            flag_4 = True
    if count < 3:
        return "NG"

    # 校验规则3
    for i in range(3, len(pwd) // 2 + 1):
        for j in range(0, len(pwd) - i + 1):
            length = pwd.count(pwd[j:j+i])
            if length > 1:
                return "NG"
    return "OK"


for line in sys.stdin:
    result = verify_pwd(line.strip())
    print(result)

全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务