检查键盘上相邻字符的字符串 [英] Checking a string for adjacent characters on the keyboard

查看:267
本文介绍了检查键盘上相邻字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查字符串中的连续字符,以查看它们在键盘上是否相邻.这是我正在编写的用于评估密码强度的程序的一部分.

I'm trying to check consecutive characters in a string to see if they are adjacent on the keyboard. It's part of a program that I am writing to assess password strength.

我有以下代码将键盘初始化为数组:

I have this code that initializes the keyboard as an array:

KeyboardRow1 = ["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", ""]
KeyboardRow2 = ["", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", ""] 
KeyboardRow3 = ["", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "", "", ""] 
KeyboardRow4 = ["", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "", "", ""]
KeyboardRow1S = ["~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", ""]
KeyboardRow2S = ["", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|"]
KeyboardRow3S = ["","A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "", "", ""] 
KeyboardRow4S = ["", "Z", "X", "C", "V", "B", "N", "M", "<", ">", "?", "", "", ""]
Rows = [KeyboardRow1, KeyboardRow2, KeyboardRow3, KeyboardRow4, \
        KeyboardRow1S, KeyboardRow2S, KeyboardRow3S, KeyboardRow4S]

然后我具有此功能,可以将输入的密码的每个字符转换为键盘数组内的坐标:

Then I have this function that turns each character of an entered password into coordinates inside the keyboard array:

def ConvertToCoordinates(Password):

    Coordinates = []
    for c in Password:
        for i, r in enumerate(Rows):
            try:
                Coordinates.append((i % 4, r.index(c)))
            except:
                pass
    return Coordinates

最后,这是检查相邻性的功能-我需要帮助的地方:

Finally, this is the function that checks for adjacency - where I need help:

def CheckForAdjacency(Coordinates):

    Adjacent = 0
    for pairs in combinations(Coordinates, 2):
        if (abs(pairs[0][0] - pairs[1][0]) == 1) \
           and (abs(pairs[0][1] - pairs[1][1]) == 1):
            Adjacent += 1
    return Adjacent

上述功能检查字符串中的每个字符是否相互关联,而不是仅检查字符串中彼此相邻的字符.

The above function checks each character in a string in relation to each other, as opposed to only characters that are next to each other in the string.

例如,我希望qwerty返回5个相邻字符,而qetwr返回0.如果qwerty是密码,该函数的确返回5.它会检查所有字符,而不是彼此相邻.

For example, I would want qwerty to return 5 adjacent characters and qetwr to return 0. If qwerty is the password, the function does indeed return 5. However, if qetwr is the password it also returns 5; it checks all the characters instead of characters next to each other.

如何使它仅检查字符串中彼此相邻的字符?我知道问题出在使用组合对"中,我认为这会检查所有可能的配置和配对.

How can I make it check just characters next to each other in the string? I know that the problem comes from the use of "for pairs in combinations" , I think that checks for all possible configurations and pairings.

推荐答案

我认为确定两个键是否相邻的逻辑不太正确.如果两个键相邻(或相同),则它们的x或y坐标不能大于1.

I think the logic in your determination of if two keys are adjacent was not quite right. If two keys are adjacent (or same) then their x or y coordinates cannot be greater than 1.

itertools.combinations还会生成坐标的所有组合,但是您只想计算相邻的坐标.

Also itertools.combinations generates all combination of the coordinates, but you only want to calculate the ones next to each other.

例如密码="ABCD"组合为您提供"AB AC AD BC BD CD"

e.g. password = 'ABCD' combinations give you 'AB AC AD BC BD CD'

def isAdjacent(Coord1, Coord2):
    if abs(Coord1[0] - Coord2[0]) > 1 or abs(Coord1[1] - Coord2[1]) > 1:
        return False
    else:
        return True

def CheckForAdjacency(Coordinates):
    Adjacent = 0
    for i in range(len(Coordinates) - 1):
        if isAdjacent(Coordinates[i], Coordinates[i +1]):
            Adjacent += 1
    return Adjacent

这篇关于检查键盘上相邻字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆