题解 | #密码合格# 把条件三“包含公共元素”去掉就好理解

密码验证合格程序

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

import sys

def check_password(password):
    # 长度超过8位
    if len(password) <= 8:
        return False

    # 包括大小写字母、数字、其他符号(不含空格或换行)
    categories_count = 0
    for char in password:
        if char.isalpha():
            if char.islower():
                categories_count |= 1  # 小写字母
            else:
                categories_count |= 2  # 大写字母
        elif char.isdigit():
            categories_count |= 4  # 数字
        elif char.isascii() and not char.isspace():
            categories_count |= 8  # 其他符号

    # 以上四种至少三种
    if bin(categories_count).count("1") < 3:
        return False

    # 不能有长度大于2的包含公共元素的子串重复
    for i in range(len(password) - 2):
        substr1 = password[i : i + 3]
        # print(set(substr1)&set(substr2))
        if password.count(substr1) > 1:
            return False

    return True


# 示例使用
# password = []
# for line in sys.stdin:
#     _password = line.strip('\n')
#     if _password == '':
#         break
#     password.append(_password)

# for _ in password:
#     if check_password(_):
#         print("OK")
#     else:
#         print("NG")


password = input()

if check_password(password):
    print("OK")
else:
    print("NG")

全部评论

相关推荐

03-27 17:33
门头沟学院 Java
代码飞升:同学院本,你要注意hr当天有没有回复过,早上投,还要打招呼要推销自己,不要一个劲投
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务