Skip to content

Instantly share code, notes, and snippets.

@jsomers
Created February 4, 2011 15:02
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 jsomers/811194 to your computer and use it in GitHub Desktop.
Save jsomers/811194 to your computer and use it in GitHub Desktop.
A script that finds unique word templates
words = File.open("./TWL06.txt").read.downcase.gsub("\r", "").split("\n")
words.select! {|w| w.length == 6}
# Approach:
# 1. Generate an index that maps 2-templates (like "_l___x") to words ("climax").
# 2. Find 2-templates that map to just one word.
def templates(word)
"Finds all fifteen (6! / 4! * 2!) 2-templates for a given word."
inds = []; (0..4).collect {|i| (i + 1..5).each {|j| inds << [i, j]}}
ts = []
inds.each do |ij|
w = word.split('')
((0..5).to_a - ij).each { |x| w[x] = "_" }
ts << w.join
end
return ts
end
# Step 1.
index = {}
words.each do |word|
templates(word).each do |t|
if index[t] then index[t] << word else index[t] = [word] end
end
end
# Step 2.
p index.select {|k, v| v.length == 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment