马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 qq1151985918 于 2021-10-17 22:00 编辑
def isLengthEnough(s):
if 6 <= len(s) <= 20:
return True
else:
return False
def isNumIn(s):
for i in s:
if i.isdigit():
return True
else:
return False
def isLowIn(s):
for i in s:
if i.islower():
return True
else:
return False
def isCapIn(s):
for i in s:
if i.isupper():
return True
else:
return False
def isNotRepeat3(s):
for i in set(s):
if i * 3 in s:
return False
else:
return True
def allRepeat3(s):
r = []
while not isNotRepeat3(s):
for i in s:
for j in range(len(s), 2, -1):
if i * j in s:
for k in range(s.count(i * j)):
r.append(i * j)
s = s.replace(i * j, "")
break
return sorted(r, key=lambda x:len(x), reverse=True)
def chooseChar(s):
for x in allRepeat3(s[:]):
s = s.replace(x, x[:2])
return s
def run(s):
r = sum([isLengthEnough(s), isNumIn(s), isLowIn(s), isCapIn(s), isNotRepeat3(s)])
if r == 5:
result = 0
elif sum([isLengthEnough(s), isNotRepeat3(s)]) == 0:
cs = chooseChar(s)
if len(cs) >= 20:
return (len(s) - 20 + (3 - r))
if len(cs) + (3 - r) >= 20:
return (len(s) - 20 + (3 - r))
allR = allRepeat3(s)
t, flg = 0, False
res1 = max([6-len(s), len(s)-20])
n = 20 - len(cs)
for x in allR:
i = x[:-2]
if flg:
t += len(s) - len(cs) - len(allR[0][:-2])
break
elif not flg and len(i) > n:
flg = True
t += len(i) - n + (n // 3)
else:
t += len(i) // 3
res2 = max([t, 3-r])
result = max([res1, res2])
elif not isLengthEnough(s):
t = max([6-len(s), len(s)-20])
result = max([t, 4-r])
elif not isNotRepeat3(s):
t = 0
for i in allRepeat3(s):
t += len(i) // 3
result = max([t, 4-r])
else:
result = 5 - r
return result
if __name__ == "__main__":
password = input("输入密码:")
print("密码变强所需的最少步骤:", run(password))
|