My Anagram Mangler

I really like anagrams but my brain is pretty lazy. So lazy that that Python which I wrote to brute force some short anagrams in Blue Prince turned out to be too much work for me to use. So I remembered that I also know Ruby & wrote a 1.0 implementation of a new idea I had for an anagram helper. It's a tiny bit smarter than the Python version but hardcoded as hell at this stage because I didn't want to overengineer a toy.

# shanagrams.rb - index a file of strings, then return strings containing
# same letters as an input string

h = {}

def mill_grain sack
  rack = ''
  tall = sack.upcase
  letters = tall.chars
  letters.sort.each do |character|
    if character >= 'A' && character <= 'Z'
      rack << character
    end
  end
  return rack
end

File.open('/usr/share/dict/words', 'r') do |f|
  f.each_line do |line|

    spaceless = line.split()
    lookup_handle = mill_grain spaceless[0]

    if not h[lookup_handle]
      h[lookup_handle] = [spaceless]
    else
      h[lookup_handle] = h[lookup_handle] + [spaceless]
    end
  end
end

puts "string to find anagram for: "
my_word = gets
my_word.chomp
key_built = mill_grain my_word

p h[key_built]

links

social