如果字符串是强密码,则编写一个返回True的程序。
如果满足以下所有条件,则认为密码很强:
我解决了一个与LeetCode强密码检查器类似的问题。如果您想检查代码或添加其他方法或提供任何更改/改进建议,请这样做!谢谢!
def strong_password_match(s: str) -> bool:
"""Returns a boolean if a password is correct"""
import re
if re.match(
r'^(?!.*([a-z])\1\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{6,20}输出re.match Benchmark: 1.8437564770000001
re.match: "abcAb1" is a strong password.
re.match Benchmark: 1.9900512190000006
re.match: "abcAb1abcAb1abcAb100" is a strong password.
re.match Benchmark: 1.782858269
re.match: "abcAb1abcAb1abcAb10111" is not a strong password.
re.match Benchmark: 1.8016775740000002
re.match: "aaaAb1abcAb1abcAb100" is not a strong password.
re.search Benchmark: 1.8374795240000008
re.search: "abcAb1" is a strong password.
re.search Benchmark: 1.8352807049999988
re.search: "abcAb1abcAb1abcAb100" is a strong password.
re.search Benchmark: 1.8023251919999996
re.search: "abcAb1abcAb1abcAb10111" is not a strong password.
re.search Benchmark: 1.9213070860000006
re.search: "aaaAb1abcAb1abcAb100" is not a strong password.
re.findall Benchmark: 1.7806425130000019
re.findall: "abcAb1" is a strong password.
re.findall Benchmark: 1.836686480000001
re.findall: "abcAb1abcAb1abcAb100" is a strong password.
re.findall Benchmark: 1.8053996060000017
re.findall: "abcAb1abcAb1abcAb10111" is not a strong password.
re.findall Benchmark: 1.8203372360000003
re.findall: "aaaAb1abcAb1abcAb100" is not a strong password.如果您希望简化/修改/探索表达式,则在regex101.com的右上面板中已经解释了该表达式。如果您愿意的话,您也可以在此链接中观察它如何与一些示例输入相匹配。RegEx电路jex.im源强密码检查器, s) is not None:
return True
return False
def strong_password_search(s: str) -> bool:
"""Returns a boolean if a password is correct"""
import re
if re.search(
r'^(?!.*([a-z])\1\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{6,20}K115输出K216A17如果您希望简化/修改/探索表达式,则在C18的右上面板中已经解释了该表达式。如果您愿意的话,您也可以在C19中观察它如何与一些示例输入相匹配。K120RegEx电路K221C22B23K124源K225C26, s) is not None:
return True
return False
def strong_password_findall(s: str) -> bool:
"""Returns a boolean if a password is correct"""
import re
if re.findall(
r'^(?!.*([a-z])\1\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{6,20}K115输出K216A17如果您希望简化/修改/探索表达式,则在C18的右上面板中已经解释了该表达式。如果您愿意的话,您也可以在C19中观察它如何与一些示例输入相匹配。K120RegEx电路K221C22B23K124源K225C26, s) != []:
return True
return False
if __name__ == '__main__':
# ---------------------------- TEST ---------------------------
import timeit
import cProfile
DIVIDER_DASH_LINE = '-' * 50
GREEN_APPLE = '\U0001F34F'
RED_APPLE = '\U0001F34E'
test_methods = (
("re.match", strong_password_match),
("re.search", strong_password_search),
("re.findall", strong_password_findall),
)
test_inputs = ('abcAb1', 'abcAb1abcAb1abcAb100',
'abcAb1abcAb1abcAb10111', 'aaaAb1abcAb1abcAb100')
# --------------------------------- PROFILING AND BANCHMARK SETTINGS --------------------------------------
NUMBER_OF_RUNS = 1
CPROFILING_ON = False
BENCHMARK_ON = True
for description, method in test_methods:
print((GREEN_APPLE + RED_APPLE) * 5)
for test_input in test_inputs:
if CPROFILING_ON:
print(f'{description} cProfiling: ', cProfile.run("method(test_input)"))
if BENCHMARK_ON:
print(f'{description} Benchmark: ', timeit.Timer(
f'for i in range({NUMBER_OF_RUNS}): {method(test_input)}', 'gc.enable()').timeit())
if method(test_input):
print(f'{GREEN_APPLE} {description}: "{test_input}" is a strong password.')
else:
print(f'{RED_APPLE} {description}: "{test_input}" is not a strong password.')K115输出K216A17
如果您希望简化/修改/探索表达式,则在C18的右上面板中已经解释了该表达式。如果您愿意的话,您也可以在C19中观察它如何与一些示例输入相匹配。
K120RegEx电路K221
C22
B23K124源K225
C26
发布于 2019-10-24 22:24:36
我注意到您导入re三次,每次输入一次密码检查函数。为什么?只需在文件顶部导入库一次,就可以在所有函数中使用它。
与其根据表达式的结果返回True或False,不如直接返回表达式。结果无论如何都是布尔值,因此返回True和False是不必要的;只需返回表达式即可。
您有三个函数,它们本质上执行相同的任务,只是函数调用稍有不同。您可以将这些regex检查压缩到一个方法中。然后,您可以通过要对密码执行的检查类型。见下文。
import re
def strong_password_check(password: str, type_of_check: str) -> bool:
"""
Accepts a password and the type of check to perform on the password
:param password: Password to check
:param type_of_check: How to check the password. Has to be "match", "search", or "findall"
"""
regex = r'^(?!.*([a-z])\1\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{6,20}测试用例不太确定红苹果和绿苹果有多有用。同样,导入cProfile和timeit应该位于程序的首位。我看到你在检查性能,但没有检查密码的实际强度。你的程序会同意Password123是一个好密码吗?任何值钱的保安专家都不会同意。我建议您使用Kolmgorov复杂性检查密码的强度。
if type_of_check == "match":
return re.match(regex, password) is not None
if type_of_check == "search":
return re.search(regex, password) is not None
if type_of_check == "findall":
return re.findall(regex, password) != []K112测试用例K213
不太确定红苹果和绿苹果有多有用。同样,导入D14和D15应该位于程序的首位。
我看到你在检查性能,但没有检查密码的实际强度。你的程序会同意D16是一个好密码吗?任何值钱的保安专家都不会同意。
我建议您使用C17检查密码的强度。
https://codereview.stackexchange.com/questions/231214
复制相似问题