Jun 012018

My local radio station has a quiz every once in a while. The questions are hardly “University Challenge”, but there is one question that always is fiendishly tricksy and slippery: to identify a place in the county from an anagram of it.

Constantly getting all but one question correct, it is, of course time to cheat use lateral thinking.

#1 get a list of place names. A gazetteer should do.

#2 Build an index (hash). First thoughts were to md5 the names, but there’s an easier way: equalise case, remove spaces and punctuation, order the string…

places = Hash.new([])

File.open("./places.txt", "r") do |file|
  while line = file.gets
    place = line.chomp
    # kill possesives!!
    places_hash = line.chomp.downcase.delete(' ').delete("'")
    places[places_hash.chars.sort.join]+= [place]

File.open("places_hash", "w") do |file|
  Marshal.dump(places, file)

puts places.inspect

#3 Simply pull out the entry that matches the key…

places = nil

File.open("places_hash", "r") do |file|
  places = Marshal.load(file)

wrangler = "REPLACE_ME"

rewrangler = wrangler.downcase.delete(' ').delete("'")
sorted_wrangler = rewrangler.chars.sort.join
answer = places[sorted_wrangler]

answer = answer[0] ||= "nowhere to be found in Shropshire!"
puts "wrangler: #{wrangler} \n\nIt's probably... #{answer}"

And that’s all she wrote… a basic anagram solver.

program output

program output

Leave a Reply