Skip to content

Instantly share code, notes, and snippets.

@sharpobject
Last active July 25, 2018 22:42
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 sharpobject/fd68e2206833a63a680c4b810d78b6e9 to your computer and use it in GitHub Desktop.
Save sharpobject/fd68e2206833a63a680c4b810d78b6e9 to your computer and use it in GitHub Desktop.
local bylen = {}
for i=1,99 do bylen[i] = {} end
for word in io.lines("/usr/share/dict/words") do
if #word % 2 == 0 then
local bucket = bylen[#word]
bucket[#bucket+1] = word:lower()
end
end
for i=2,99,2 do
local prefixes = {}
local suffixes = {}
local joints = {}
local half = i/2
for _,word in ipairs(bylen[i]) do
local prefix = word:sub(1,half)
local suffix = word:sub(half+1)
prefixes[prefix] = true
suffixes[suffix] = true
end
for prefix,_ in pairs(prefixes) do
if suffixes[prefix] then
joints[prefix] = {{},{}}
end
end
for _,word in ipairs(bylen[i]) do
local prefix = word:sub(1,half)
local suffix = word:sub(half+1)
if joints[prefix] then
joints[prefix][2][word] = true
end
if joints[suffix] then
joints[suffix][1][word] = true
end
end
for _,stuff in pairs(joints) do
for first, _ in pairs(stuff[1]) do
for second, _ in pairs(stuff[2]) do
print(first, second)
end
end
end
end
@sharpobject
Copy link
Author

removed dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment