正则匹配与长度3的子串重复
密码验证合格程序
http://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841
构造一个检查函数 checkLegal
1)生成一个长度为3的所有子串序列(因为长度大于4的相同子串,必定存在长度3的相同子串);
2)if len(set(sub)) < len(sub):return False python 用set去重,判断长度就可以知道是否有重复;
3)类型用正则匹配,如果匹配到目标数据格式 type_ += 1;
4)最后return True if type_ >= 3 else False;
5)处理多行输入输出,调用该函数即可。
def checkLegal(pswd): if len(pswd) <= 8:return False else: #最大重复子串长度2+ sub = [] for i in range(len(pswd)-2): sub.append(pswd[i:i+3]) if len(set(sub)) < len(sub):return False #check type 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, pswd) if pw : type_ += 1 return True if type_ >= 3 else False while True: try: pswd = input() print('OK' if checkLegal(pswd) else 'NG') except: break