Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created November 12, 2022 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baweaver/900594ffafa0095f2d1e442084018704 to your computer and use it in GitHub Desktop.
Save baweaver/900594ffafa0095f2d1e442084018704 to your computer and use it in GitHub Desktop.
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