【问题标题】:Comparing a string with elements in a list将字符串与列表中的元素进行比较
【发布时间】:2019-06-19 11:26:25
【问题描述】:

我正在尝试编写密码强度检查器,如果输入的密码是常见的键盘组合,例如“qwerty”或“asdfg”,我想扣分。我有一个['q', 'w', 'e', ... 'b', 'n', 'm'] 的列表。如果输入的任何部分具有列表中的连续元素,我想扣分。假设密码是“djoDFGibTY”(大写只是为了突出显示,全部小写),我希望我的代码捕捉到“DFG”和“TY”并扣分两次,在第一种情况下扣分更多,因为三次违规和在第二种情况下,双重违规的情况较轻。谢谢。

keyboard_pattern = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm']

if password in keyboard_pattern:

score -= 15

【问题讨论】:

  • with more points deducted in the first case for a triple violation and lesser in the second case for a double violation,第一种情况多少分,第二种情况多少分
  • 假设两个连续的字符,我想要一个 -5;对于三个连续的字符 a -7;等等。
  • codereview.stackexchange.com/questions/177415/… 这能回答你的要求吗?
  • 这不处理 2 个连续或 4 个连续字符,而只处理 3 个连续字符

标签: python python-3.x string list comparison


【解决方案1】:

https://codereview.stackexchange.com/questions/177415/python-qwerty-keyboard-checker上发现

input = input("What is your password?")
qwerty = 'qwertyuiopasdfghjklzxcvbnm'
lower = input.lower()
for idx in range(0, len(lower) - 2):
    test_seq = lower[idx:idx + 3]
    if test_seq in qwerty:
        points -= 5
print(points)

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多