“Random Facebook quiz” (part 2/3)

PhiPi from the Propeller forums pointed out that my simple id() function would treat the words “MOW” and “LOX” as the same. This means my code “accidently worked” for the dataset provided in the question.

PhiPi made a suggestion that I ran with. I tweaked the id() function and came up with this:


t = { "tsar", "rat", "tar", "star", "tars", "cheese", }
function id(s)
local t = {}
for i=1,26 do t[i] = 0 end
for i=1,#s do
local c = string.lower(s:sub(i,i))
if c >= "a" and c <= "z" then local h = string.byte(c) - ("a"):byte() + 1 if h > 0 then t[h] = t[h] + 1 end
end
end
local r = ""
for i=1,26 do
if t[i] > 0 then
r = r .. string.char(("a"):byte() + i - 1)
end
if t[i] > 1 then
r = r .. t[i]
end
end
return r
end
p = {} u = {}
print("* word -> id *")
for i=1,#t do
p[i] = 0
u[i] = id(t[i])
print(t[i], u[i])
end
for i=1,#u do
if p[i] == 0 then
p[i] = 1
print("===")
print(t[i])
for j=i+1,#u do
if p[j] == 0 and u[i] == u[j] then
p[j] = 1
print(t[j])
end
end
end
end

which outputs:


* word -> id *
tsar arst
rat art
tar art
star arst
tars arst
cheese ce3hs
===
tsar
star
tars
===
rat
tar
===
cheese

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.