Textonyms: Difference between revisions

2,054 bytes added ,  8 years ago
Added Lua version
(Added Lua version)
Line 1,036:
4317 (10) GE's, Gd's, Ge's, HF's, He's, Hf's, ID's, he's, id's, if's
</pre>
 
=={{header|Lua}}==
<lang Lua>-- Global variables
http = require("socket.http")
keys = {"VOICEMAIL", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
dictFile = "http://www.puzzlers.org/pub/wordlists/unixdict.txt"
 
-- Return the sequence of keys required to type a given word
function keySequence (str)
local sequence, noMatch, letter = ""
for pos = 1, #str do
letter = str:sub(pos, pos)
for i, chars in pairs(keys) do
noMatch = true
if chars:match(letter) then
sequence = sequence .. tostring(i)
noMatch = false
break
end
end
if noMatch then return nil end
end
return tonumber(sequence)
end
 
-- Generate table of words grouped by keySequence
function textonyms (dict)
local combTable, keySeq = {}
for word in dict:gmatch("%S+") do
keySeq = keySequence(word)
if keySeq then
if combTable[keySeq] then
table.insert(combTable[keySeq], word)
else
combTable[keySeq] = {word}
end
end
end
return combTable
end
 
-- Analyse sequence table and print details
function showReport (keySeqs)
local wordCount, seqCount, tCount = 0, 0, 0
for seq, wordList in pairs(keySeqs) do
wordCount = wordCount + #wordList
seqCount = seqCount + 1
if #wordList > 1 then tCount = tCount + 1 end
end
print("There are " .. wordCount .. " words in " .. dictFile)
print("which can be represented by the digit key mapping.")
print("They require " .. seqCount .. " digit combinations to represent them.")
print(tCount .. " digit combinations represent Textonyms.")
end
 
-- Main procedure
showReport(textonyms(http.request(dictFile)))</lang>
{{out}}
<pre>There are 24983 words in http://www.puzzlers.org/pub/wordlists/unixdict.txt
which can be represented by the digit key mapping.
They require 22908 digit combinations to represent them.
1473 digit combinations represent Textonyms.</pre>
 
=={{header|Perl}}==
Anonymous user