密码验证合格程序python3
密码验证合格程序
http://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841
import sys class CheckPasswd(): def __init__(self, passwd): self.passwd = passwd self.type_counter = 0 self.child_str = False def check_type(self): #检查类型是否多于等于3 type_set = set() for i in self.passwd: if i.isdigit(): #是否有数字 type_set.add(1) elif i.isalpha(): #是否有字母 if i.lower() == i: type_set.add(2) else: type_set.add(3) else: type_set.add(4) self.type_counter = len(type_set) def check_child(self): #检查是否有重复子串 for i in range(len(self.passwd)-3): if self.passwd.count(self.passwd[i:i+3]) > 1: self.child_str = True break #这里一检查到有重复子串,就break,不然一直遍历,最后的结果可能为False else: self.child_str = False def run_check(self): if len(self.passwd) <= 8: return 'NG' self.check_type() if self.type_counter < 3: return 'NG' self.check_child() if self.child_str: return 'NG' return 'OK' def main(): while True: passwd = sys.stdin.readline().strip() if passwd == '': break check_instance = CheckPasswd(passwd) print(check_instance.run_check()) main()