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

密码验证合格程序

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

import sys

# 读取全部input,并且根据换行符切割新password
passwords = sys.stdin.read().strip().splitlines()

def length_check(password):
    # 检查密码长度是否在规定长度内
    return "OK" if 8 < len(password) < 100 else "NG"

def variety_check(password):
    # 检查是否存在4种中至少3种特殊符号
    # 大小写,数字,特殊字符(不包含空格)
    has_upper = any(char.isupper() for char in password)
    has_lower = any(char.islower() for char in password)
    has_digit = any(char.isdigit() for char in password)
    has_special = any(not char.isalnum() for char in password)

    # 统计一共多少种不同的字符存在
    total_types = sum([has_upper, has_lower, has_digit, has_special])
    return "OK" if total_types >= 3 else "NG"

def duplicate_check(password):
    # 检查是否存在超过3个的重复字母
    for i in range(len(password) - 2):
        if password[i:i + 3] in password[i + 1:]:
            return "NG"  # If a duplicate substring is found
    return "OK"

# 检查每个密码并且打印最终结果
for password in passwords:
    if (
        length_check(password) == "OK" and
        variety_check(password) == "OK" and
        duplicate_check(password) == "OK"
    ):
        print("OK")
    else:
        print("NG")

全部评论

相关推荐

点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务