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'

defisAdjacent(Coord1, Coord2):
    ifabs(Coord1[0] - Coord2[0]) > 1orabs(Coord1[1] - Coord2[1]) > 1:
        returnFalseelse:
        returnTruedefCheckForAdjacency(Coordinates):
    Adjacent = 0for i inrange(len(Coordinates) - 1):
        if isAdjacent(Coordinates[i], Coordinates[i +1]):
            Adjacent += 1return Adjacent

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