gpt4 book ai didi

python - 统计多个子串同时出现在一个字符串中的次数

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:23 27 4
gpt4 key购买 nike

我正在用 Python 编写一个简单的脚本,用于在评分系统上评估密码的强度,评分系统根据密码是否包含学校的大写或小写字母、数字和符号来给分和取分。

其中一个要求是检查英国标准键盘上从左到右连续的 3 个字母或数字,每个实例扣 5 分。例如,密码“qwer123”会因为“qwe”、“wer”和“123”而失去 15 分。这怎么可能实现呢?我当前的代码如下。

def check():
user_password_score=0
password_capitals=False
password_lowers=False
password_numbers=False
password_symbols=False
password_explanation_check=False
ascii_codes=[]
password_explanation=[]
print("The only characters allowed in the passwords are upper and lower case letters, numbers and these symbols; !, $, %, ^, &, *, (, ), _, -, = and +.\n")
user_password=str(input("Enter the password you would like to get checked: "))
print("")
if len(user_password)>24 or len(user_password)<8:
print("That password is not between 8 and 24 characters and so the Password Checker can't evaluate it.")
menu()
for i in user_password:
ascii_code=ord(i)
#print(ascii_code)
ascii_codes.append(ascii_code)
#print(ascii_codes)
for i in range(len(ascii_codes)):
if ascii_codes[i]>64 and ascii_codes[i]<90:
password_capitals=True
elif ascii_codes[i]>96 and ascii_codes[i]<123:
password_lowers=True
elif ascii_codes[i]>47 and ascii_codes[i]<58:
password_numbers=True
elif ascii_codes[i] in (33,36,37,94,38,42,40,41,45,95,61,43):
password_symbols=True
else:
print("Your password contains characters that aren't allowed.\n")
menu()
if password_capitals==True:
user_password_score+=5
if password_lowers==True:
user_password_score+=5
if password_numbers==True:
user_password_score+=5
if password_symbols==True:
user_password_score+=5
if password_capitals==True and password_lowers==True and password_numbers==True and password_symbols==True:
user_password_score+=10
if password_numbers==False and password_symbols==False:
user_password_score-=5
if password_capitals==False and password_lowers==False and password_symbols==False:
user_password_score-=5
if password_capitals==False and password_lowers==False and password_numbers==False:
user_password_score-=5
#print(user_password_score)
if user_password_score>20:
print("Your password is strong.\n")
else:
print("That password is weak.\n")
#don't forget you still need to add the thing that checks for 'qwe' and other stuff.
menu()

最佳答案

您可以将禁止序列存储在一组字符串中,并在每次有人使用该序列时递减分数。

password = "qwert123"
score = 42 # initial score
sequences = { # all in lowercase because of the `lower()` in the loop
"qwertyuiopasdfghjklzxcvbnm",
"azertyuiopqsdfghjklmwxcvbn",
"abcdefghijklmnopqrstuvwxyz",
"01234567890"
}
match_length = 3 # length threshold for the sanction
sequences.update({s[::-1] for s in sequences}) # do we allow reverse ?

for c in range(len(password)-match_length+1):
for seq in sequences:
if password[c:c+match_length].lower() in seq:
score-=5
print(f"'{password[c:c+match_length]}' => -5 !")
break # Don't flag the same letters more than once

print(score) # 22 (42-4*5)

关于python - 统计多个子串同时出现在一个字符串中的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com