Checking a string for adjacent characters on the k

2019-09-12 04:49发布

问题:

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.

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.

回答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.

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

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