Skip to content

Instantly share code, notes, and snippets.

@petertseng
Last active November 1, 2015 18:07
Show Gist options
  • Save petertseng/22eecf088be16086ed69 to your computer and use it in GitHub Desktop.
Save petertseng/22eecf088be16086ed69 to your computer and use it in GitHub Desktop.
Upside down words!
dod -> pop
dollop -> dollop
dot -> top
dots -> stop
loot -> tool
loots -> stool
los -> sol
nu -> nu
otto -> otto
plot -> told
pod -> pod
pop -> dod
sol -> los
solos -> solos
stool -> loots
stop -> dots
stunts -> stunts
suns -> suns
tn -> ut
told -> plot
tool -> loot
toot -> toot
top -> dot
tot -> tot
un -> un
usn -> usn
ut -> tn
require 'set'
UPSIDE_DOWN = {
?b => ?q,
?d => ?p,
?n => ?u,
?l => ?l,
?o => ?o,
?s => ?s,
?t => ?t,
?x => ?x,
}
UPSIDE_DOWN.merge!(UPSIDE_DOWN.invert)
UPSIDE_DOWN.freeze
regex = /^[#{UPSIDE_DOWN.keys.join}]{2,}$/
word_list = File.readlines('/usr/share/dict/cracklib-small').map(&:chomp).grep(regex)
word_set = Set.new(word_list)
def upside_down(w)
w.chars.map { |c| UPSIDE_DOWN[c] }.join.reverse
end
word_list.select! { |w| word_set.include?(upside_down(w)) }
puts word_list.map { |w| "#{w} -> #{upside_down(w)}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment