题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
def checkpsw(psw):
#1密码长度大于8
if len(psw)<=8: return False
else:
#2最大重复子串长度2+
sub = []
for i in range(len(psw)-2):
sub.append(psw[i:i+3]) #一次取两个,加入列表中
if len(set(sub)) < len(sub):return False #利用set的无重复的性质,如果存在重复,肯定长度变小
#3查看数字类型
type_ = 0
import re #引入正则表达式
Upper = '[A-Z]'
Lowwer = '[a-z]'
num = '\d'
chars = '[^A-Za-z0-9_]'
patterns = [Upper, Lowwer, num, chars]
for pattern in patterns:
pw = re.search(pattern, psw) #在psw中查找相关字段,re.reserch(pattern, string, flags=0)
if pw : type_ += 1
return True if type_ >= 3 else False
while True:
try:
psw = input()
print('OK' if checkpsw(psw) else 'NG')
except:
break