Created
November 12, 2022 11:04
-
-
Save baweaver/900594ffafa0095f2d1e442084018704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KEYBOARD = [ | |
'1234567890-='.chars, | |
'qwertyuiop[]'.chars, | |
'asdfghjkl;\''.chars, | |
'zxcvbnm,./'.chars, | |
] | |
def reasonable_typos(distance: 3) | |
typo_map = Hash.new do |h, initial_key| | |
h[initial_key] = {} | |
end | |
KEYBOARD.each_with_index do |row, x| | |
row.each_with_index do |key, y| | |
distance.times do |initial_offset| | |
offset = initial_offset + 1 | |
[ | |
[x + offset, y], # Right | |
[x - offset, y], # Left | |
[x, y + offset], # Up | |
[x, y - offset], # Down | |
[x + offset, y + offset], # Up Right | |
[x + offset, y - offset], # Down Right | |
[x - offset, y + offset], # Up Left | |
[x + offset, y - offset], # Down Left | |
].each do |x2, y2| | |
next if x2.negative? || y2.negative? | |
new_key = KEYBOARD.dig(x2, y2) | |
next if new_key.nil? | |
next if typo_map[key].key?(new_key) | |
typo_map[key][new_key] = offset | |
end | |
end | |
end | |
end | |
typo_map | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment