Skip to content Skip to sidebar Skip to footer

Checking A String For Adjacent Characters On The Keyboard

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 thi

Solution 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

Post a Comment for "Checking A String For Adjacent Characters On The Keyboard"