题目介绍
题解1:逐一遍历匹配
while True:
try:
pw = input()
a, b, c, d, flag = 0, 0, 0, 0, True
for i in pw:
if i.isdigit():
a = 1
elif i.islower():
b = 1
elif i.isupper():
c = 1
else:
d = 1
for i in range(len(pw) - 3):
if pw.count(pw[i:i + 3]) > 1:
flag = False
break
if len(pw) > 8 and a + b + c + d >= 3 and flag:
print("OK")
else:
print("NG")
except EOFError:
break
题解2:使用正则匹配
import re
def check_pw(pw):
if len(pw) < 8:
return False
mix_types = 0
if re.search(r'\d', pw):
mix_types += 1
if re.search('[a-z]', pw):
mix_types += 1
if re.search('[A-Z]', pw):
mix_types += 1
if re.search('[^0-9a-zA-Z]', pw):
mix_types += 1
if mix_types < 3:
return False
for i in range(len(pw) - 3):
if pw.count(pw[i:i + 3]) > 1:
return False
return True
try:
while True:
print('OK' if check_pw(input()) else 'NG')
except (EOFError, ValueError):
pass
|