华为机试:密码验证合格程序
主要运用面向对象构建类,解决
import sys
class checkpwd():
#initial object
type_num=0
child_str=False
def __init__(self,pwd):
self.pwd=pwd
#check this password conclude how many string types
def check_type(self):
typeset=set()
for i in self.pwd:
if i.isdigit():typeset.add('number') #number included
elif i.isalpha():
if i.islower():typeset.add('loweral') #lower alphabet included
else:typeset.add('upperal') #upper alphabet included
else:typeset.add('others') #others' character
self.type_num=len(typeset) #strings' type number
#check whether this password concludes repeat strings
def check_child(self):
for i in range(len(self.pwd)-3): #check strings including 3 characters
if self.pwd.count(self.pwd[i:i+3])>1:
self.child_str=True #including repeat strings
break
else:self.child_str=False #not including
#check password globally
def check_pwd(self):
if len(self.pwd)<=8:return 'NG'
self.check_child()
if self.child_str:return 'NG'
self.check_type()
if self.type_num<3:return 'NG'
return 'OK'
while True:
pwd=sys.stdin.readline().strip() #input strings,cut space,etc. in the end
if pwd=='':break
cp=checkpwd(pwd)
print(cp.check_pwd())
京公网安备 11010502036488号