Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created May 5, 2012 15:31
Show Gist options
  • Save robdodson/2603361 to your computer and use it in GitHub Desktop.
Save robdodson/2603361 to your computer and use it in GitHub Desktop.
Anagrams in Ruby
#!/usr/bin/env ruby -wKU
require 'optparse'
dictionary = '/usr/share/dict/words'
OptionParser.new do |opts|
opts.banner = 'Usage: anagram [ options ] word...'
opts.on('-d', '--dict path', String, 'Path to dictionary') do |dict|
dictionary = dict
end
opts.on('-h', '--help', 'Show this message') do
puts opts
exit
end
begin
ARGV << '-h' if ARGV.empty?
opts.parse!(ARGV)
rescue OptionParser::ParseError => e
STDERR.puts e.message, '\n', opts
exit(-1)
end
end
# convert "wombat" into "abmotw". All anagrams share a signature
def signature_of(word)
word.unpack("c*").sort.pack("c*")
end
signatures = Hash.new
File.foreach(dictionary) do |line|
word = line.chomp
signature = signature_of(word)
(signatures[signature] ||= []) << word
end
ARGV.each do |word|
signature = signature_of(word)
if signatures[signature]
puts "Anagrams of #{word}: #{signatures[signature].join(', ')}"
else
puts "No anagrams of #{word} in #{dictionary}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment