本帖最后由 qq1151985918 于 2021-10-17 18:21 编辑
这题目非常弯弯绕,即使我几经修改仍然认为可能存在BUG,如果错误请提供案例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 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:
res1 = 6 - len(s) if len(s) < 6 else len(s) - 20
t, flg, allR = 0, False, allRepeat3(s)
for i in allR:
if flg:
t += len(s) - len(allR[0])
break
elif not flg and len(i) > 20:
flg = True
t += len(i) - 20 + (20 // 3)
else:
t += len(i) // 3
res2 = t if t > 3 - r else 3 - r
result = res1 if res1 > res2 else res2
elif not isLengthEnough(s):
t = 6 - len(s) if len(s) < 6 else len(s) - 20
result = t if t > 4 - r else 4 - r
elif not isNotRepeat3(s):
t = 0
for i in allRepeat3(s):
t += len(i) // 3
result = t if t > 4 - r else 4 - r
else:
result = 5 - r
return result
if __name__ == "__main__":
password = input("输入密码:")
print("密码变强所需的最少步骤:", run(password))
|