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

密码验证合格程序

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

def test(password):
    if len(password) < 9:
        return False

    types = {"number", "lower", "upper", "others"}
    seen_substrings = password
    # Check for at least three types of characters
    type_count = set()
    for char in password:
        if "0" <= char <= "9":
            type_count.add("number")
        elif "a" <= char <= "z":
            type_count.add("lower")
        elif "A" <= char <= "Z":
            type_count.add("upper")
        else:
            type_count.add("others")

    if len(type_count) < 3:
        return False

    # Check for repeated substrings of length greater than 2
    for i in range(len(password)-2):
            substring = password[i : i + 3] 
            if seen_substrings.count(substring)>1:
                return False

    return True

while True:
    try:
        password = input()
        if test(password):
            print("OK")
        else:
            print("NG")
    except EOFError:
        break

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 16:11
拼多多 Java工程师 26.0k*18.0
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务