题解 | #密码合格# 把条件三“包含公共元素”去掉就好理解
密码验证合格程序
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")