题解(适合萌新 )| #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
import re def kind_is_ok(strs): # 判断种类是否大于等于3种 kind = [0, 0, 0, 0] for s in strs: m = kind.count(0) if m <= 1: # 0的数量小于等于1,说明已经有三种了 return True else: if s.isdecimal(): kind[0] += 1 continue if re.match("[A-Z]", s): kind[1] += 1 continue if re.match("[a-z]", s): kind[2] += 1 continue if re.match("[a-z]", s): kind[2] += 1 continue if re.match(r"[^a-zA-Z0-9\\s]", s): kind[3] += 1 continue return False def is_not_duplicated(strs): # 判断长度大于2的子串是否有重复,即判断每三个子串是否有相同 three_list = [] for i in range(len(strs) - 2): three = strs[i] + strs[i + 1] + strs[i + 2] three_list.append(three) drop_duplicated = set(three_list) # 用集合去重 if len(three_list) != len(drop_duplicated): # 长度不一,说明有重复 return False # 返回 False 说明:不是不重复,即有重复(这样写是为了后面好判断) else: return True # 返回 True 说明不重复 if __name__ == "__main__": while True: try: item = input() n = len(item) if n > 8 and kind_is_ok(item) and is_not_duplicated(item): print("OK") else: print("NG") except: break