# Coding problem

Trying to make a function that finds the concatenation of words in a table or array.
So basically in the table below the answer printed should be catsdog because cats ,dog, and catsdog are in the table
I made a simple function to do this but how do I make a better function so that if I run the function with any string table it finds the answer without me having to manually put in x=2,y=3…etc

``````function setup()
input = {"cat","cats","dog","catsdog",}
findconcat(input)
end

function findconcat(array)
x=2
y=3
z=4
if array[x]..array[y]==array[z] then
print(array[z])
end
end
``````

@Jarc You’re going to have to do multiple loops thru the table checking each word with each other. Or you could create a string and do string searches. There are a few problems I see with this. What should you do if you have words like {art,he,ear,hear,heart,heartbeat}. If a word is matched, should it be used again somewhere else. This isn’t exactly what I’m thinking of, but one word could be made of multiple other words that are also made of smaller words.

@Jarc Thanks for a very interesting problem. It kept me busy for awhile. I originally wrote the code using a brute force method. It worked, but it would take a long time as the number of words in the table increased. What I was doing was to create a phrase using a combination of all the words and compare that phrase to the target word. It worked, but like I said, as the list increased, so did the time. Plus the code was quite large. Once I knew it could work, I had to change how I was doing it. Instead of creating phrases to compare, I would take the target word and then take each word in the table and see if it was within the target and get the correct combination of words to match the target. That also worked, but the code was again large. That’s when I decided to use recursive code which I don’t do very often. That’s what I liked about your problem, I was able to use recursion. So here’s the code. It’s a lot smaller that my original versions. The word table is shown vertically on the screen and the word combinations are shown in the print area. The target word (each word in the table) is shown in ( ) and the other table words are separated with a —.

It’s coded now to use your original word table which doesn’t do much, but you can comment out the wordTabs to get to my table that I used to write the code. That uses a lot of word combinations. I added a few more letters/word to your original table for the second wordTab as you work towards mine.

``````Updated code below
``````

``````displayMode(STANDARD)

function setup()
fill(255)
wordTab={"be","at","b","hear","beat","bart","heart","ar","t",
"he","art","eat","h","ear","heartbeat","heat"}

wordTab = {"cat","cats","dog","catsdog","at","c","do","g","s"}

wordTab = {"cat","cats","dog","catsdog"}

--table.sort(wordTab)
for z=1,#wordTab do
printTab={}
wordFind(1,wordTab[z],printTab)
end
txt=table.concat(wordTab,"\
\
")
end

function wordFind(startPos,word,pTab)
for z=1,#wordTab do
if wordTab[z]~=word then
local s,e=string.find(word,wordTab[z],startPos,true)
if s==startPos then
table.insert(pTab,wordTab[z])
if e==#word then
print("("..word..") ",table.concat(pTab,"-"))
table.remove(pTab)
else
wordFind(e+1,word,pTab)
end
end
end
end
table.remove(pTab)
end

function draw()
background(0)
text(txt,WIDTH/2,HEIGHT/2)
end
``````