Anagram Logic/Syntax

Hey all!

Im pretty new to corona and especially Lua. I’m having some trouble with strings and how to execute different operations. I’m trying to make a function that checks two words to see if they are anagrams.

Here is what I have right now:

function main() local word1 = "rat" local word2 = "format" if (isAnagram(word1, word2) == true) then local myText = display.newText("They are anagrams", 30, 30, native.systemFont, 20) myText:setTextColor(0, 255, 0) else local myText = display.newText("They are not anagrams", 30, 30, native.systemFont, 20) myText:setTextColor(0, 255, 0) end end function isAnagram(first, second) counter = 0 for i = 0, string.len(first) do for j = 0, string.len(second) do if (string.byte(first, i) == string.byte(second, j)) then local myCharNum = string.byte(first, i) local myChar = string.char(myCharNum) string.gsub(first, myChar, "0", 1) counter = counter + 1 end end end if (counter \>= string.len(first)) then return true else return false end end main()

I don’t know what is quite wrong but I run this script and I get an error with the “local myChar = string.char(myCharNum)” line.

I’m not sure if this is a newbie question but could some one please help me with this. I would be very grateful.

Thanks!

-Tad

I did a quick google to see if I could find some anagram code because I think it’s a bit more complex than that, and I found a site that seems to have anagram code in all kinds of languages, including Lua.  Enjoy:

http://rosettacode.org/wiki/Anagrams#Lua

hmmm… Thanks Rob :smiley:

This look pretty involved… I’ll have to dive deeper and see if I can implement this…

Just a note I made this same (almost exactly besides syntax) code work in C++ so I know it works for what I need but I just want to implement it in corona.

I’m not sure about this, but would putting them into tables and sorting them alphabetically work?

local wordOne, wordTwo = "hello", "eolhl"

Create a table for each word, where each entry is one letter from the word using string.sub() Eg:

local tableOne, tableTwo = {}, {} for i=1, string.len(wordOne) do     tableOne[i] = string.sub(wordOne, i, i) end --tableOne is now {"h", "e", "l", "l", "o"} for i=1, string.len(wordTwo) do     tableTwo[i] = string.sub(wordTwo, i, i) end --tableTwo is now {"e", "o", "l", "h", "l"}

Then sort each table alphabetically

table.sort(tableOne, function (a,b)                   return (b \> a)                 end) table.sort(tableTwo, function (a,b)                   return (b \> a)                 end)

Then concatenate them to create a single word in alphabetical order

local orderedWordOne = table.concat(tableOne) --ehllo local orderedWordTwo = table.concat(tableTwo) --ehllo

Now test if they are the same:

if orderedWordOne == orderedWordTwo then     print("Anagramamania!") else     print("Houston, we have a problem.") end

I think this will work for ordinary characters, but could have problems with spaces and special characters. As I say, I’m not 100% sure about it, as I haven’t tested it out. 

Just tested it out and it worked for me. Made a single function for you:

function isAnagram(wordOne, wordTwo) local tableOne, tableTwo = {}, {} for i=1, string.len(wordOne) do tableOne[i] = string.sub(wordOne, i, i) end for i=1, string.len(wordTwo) do tableTwo[i] = string.sub(wordTwo, i, i) end table.sort(tableOne, function (a,b) return (b \> a) end) table.sort(tableTwo, function (a,b) return (b \> a) end) if table.concat(tableOne) == table.concat(tableTwo) then return true else return false end end

isAnagram("hello", "eolhl") -- true isAnagram("hello", "zolhl") --false

Thanks Alan!

I’m looking for something that finds words that are anagrams… Say for example I have a dictionary of three to six letter words then I pick one six letter word and I want to find all real dictionary word anagrams of that six letter word no matter what length the word is. Does that make sense? Like in my original code I’m checking if “rat” is an anagram of “format”.

I think I could implement your code some how though… Is there some way to make a character array with lua? So say I have the array myWord = {“h”, “e”, “l”, “l”, “o”}. Could I put this into a string? or just say ‘print(myWord)’

Thanks for all the help so far! I’m sorry if I haven’t been too clear.

-Tad

I think I could implement your code some how though… Is there some way to make a character array with lua? So say I have the array myWord = {“h”, “e”, “l”, “l”, “o”}. Could I put this into a string? or just say ‘print(myWord)’

table.concat will concatenate all elements in the table into a single string:

myWord = {"h", "e", "l", "l", "o"} myNewWord = table.concat(myword) print(myNewWord) -- prints out the word "hello"

As for your other problem, you’d need to either have a record of ALL valid dictionary words built into your app, or find some API that will let you access online dictionaries. Sorry I can’t be any more helpful than that.

That’s perfect!

As for your other problem, you’d need to either have a record of ALL valid dictionary words built into your app, or find some API that will let you access online dictionaries.

Ya I was planning on doing this… hopefully it wont be too much trouble :slight_smile:

Thanks so much for your help!

Edit: Just put this in my code and it works so perfectly!!! Thanks!

is this a valid restatement of the problem?: given a six-letter source word, find all legal words that can be formed by any subset of those 6 letters.  (and assume that you will eventually have a dictionary somewhere to refer to to check “legal”)

it sounds like your current plan is to scan the entire dictionary, and ask of each word within of length <= 6 “are you an anagram of the source word?”  that could be a big task with a good dictionary.  so it _ might _ be easier to work the problem backwards (depending on max length of source string), and form all possible permutations of the source word, then only ask of the dictionary if those are legal.

iow, don’t try to “find” the anagrams, just “make” them all, then weed out the illegals.  ??

I did a quick google to see if I could find some anagram code because I think it’s a bit more complex than that, and I found a site that seems to have anagram code in all kinds of languages, including Lua.  Enjoy:

http://rosettacode.org/wiki/Anagrams#Lua

hmmm… Thanks Rob :smiley:

This look pretty involved… I’ll have to dive deeper and see if I can implement this…

Just a note I made this same (almost exactly besides syntax) code work in C++ so I know it works for what I need but I just want to implement it in corona.

I’m not sure about this, but would putting them into tables and sorting them alphabetically work?

local wordOne, wordTwo = "hello", "eolhl"

Create a table for each word, where each entry is one letter from the word using string.sub() Eg:

local tableOne, tableTwo = {}, {} for i=1, string.len(wordOne) do &nbsp;&nbsp;&nbsp;&nbsp;tableOne[i] = string.sub(wordOne, i, i) end --tableOne is now {"h", "e", "l", "l", "o"} for i=1, string.len(wordTwo) do &nbsp;&nbsp;&nbsp;&nbsp;tableTwo[i] = string.sub(wordTwo, i, i) end --tableTwo is now {"e", "o", "l", "h", "l"}

Then sort each table alphabetically

table.sort(tableOne, function (a,b) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return (b \> a) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end) table.sort(tableTwo, function (a,b) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return (b \> a) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end)

Then concatenate them to create a single word in alphabetical order

local orderedWordOne = table.concat(tableOne) --ehllo local orderedWordTwo = table.concat(tableTwo) --ehllo

Now test if they are the same:

if orderedWordOne == orderedWordTwo then &nbsp;&nbsp;&nbsp;&nbsp;print("Anagramamania!") else &nbsp;&nbsp;&nbsp;&nbsp;print("Houston, we have a problem.") end

I think this will work for ordinary characters, but could have problems with spaces and special characters. As I say, I’m not 100% sure about it, as I haven’t tested it out. 

Just tested it out and it worked for me. Made a single function for you:

function isAnagram(wordOne, wordTwo) local tableOne, tableTwo = {}, {} for i=1, string.len(wordOne) do tableOne[i] = string.sub(wordOne, i, i) end for i=1, string.len(wordTwo) do tableTwo[i] = string.sub(wordTwo, i, i) end table.sort(tableOne, function (a,b) return (b \> a) end) table.sort(tableTwo, function (a,b) return (b \> a) end) if table.concat(tableOne) == table.concat(tableTwo) then return true else return false end end

isAnagram("hello", "eolhl") -- true isAnagram("hello", "zolhl") --false

Thanks Alan!

I’m looking for something that finds words that are anagrams… Say for example I have a dictionary of three to six letter words then I pick one six letter word and I want to find all real dictionary word anagrams of that six letter word no matter what length the word is. Does that make sense? Like in my original code I’m checking if “rat” is an anagram of “format”.

I think I could implement your code some how though… Is there some way to make a character array with lua? So say I have the array myWord = {“h”, “e”, “l”, “l”, “o”}. Could I put this into a string? or just say ‘print(myWord)’

Thanks for all the help so far! I’m sorry if I haven’t been too clear.

-Tad

I think I could implement your code some how though… Is there some way to make a character array with lua? So say I have the array myWord = {“h”, “e”, “l”, “l”, “o”}. Could I put this into a string? or just say ‘print(myWord)’

table.concat will concatenate all elements in the table into a single string:

myWord = {"h", "e", "l", "l", "o"} myNewWord = table.concat(myword) print(myNewWord) -- prints out the word "hello"

As for your other problem, you’d need to either have a record of ALL valid dictionary words built into your app, or find some API that will let you access online dictionaries. Sorry I can’t be any more helpful than that.

That’s perfect!

As for your other problem, you’d need to either have a record of ALL valid dictionary words built into your app, or find some API that will let you access online dictionaries.

Ya I was planning on doing this… hopefully it wont be too much trouble :slight_smile:

Thanks so much for your help!

Edit: Just put this in my code and it works so perfectly!!! Thanks!

is this a valid restatement of the problem?: given a six-letter source word, find all legal words that can be formed by any subset of those 6 letters.  (and assume that you will eventually have a dictionary somewhere to refer to to check “legal”)

it sounds like your current plan is to scan the entire dictionary, and ask of each word within of length <= 6 “are you an anagram of the source word?”  that could be a big task with a good dictionary.  so it _ might _ be easier to work the problem backwards (depending on max length of source string), and form all possible permutations of the source word, then only ask of the dictionary if those are legal.

iow, don’t try to “find” the anagrams, just “make” them all, then weed out the illegals.  ??