Support Forums
Help with a tricky ruby problem--array of array matching - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Ruby and Ruby on Rails (https://www.supportforums.net/forumdisplay.php?fid=55)
+---- Thread: Help with a tricky ruby problem--array of array matching (/showthread.php?tid=28222)



Help with a tricky ruby problem--array of array matching - xtownshend - 12-31-2013

Hello,

So say I have the following array of arrays:

Code:
example = [['strrngy'], ['st', 'rr', 'ngy'], ['str', 'rngy'], ['strr', 'ngy'], ['str', 'rn', 'gy'], ['st', 'rrn', 'gy'], ['st', 'rrngy'], ['strrn', 'gy']]

As you can see, example is many different versions of the word 'strrngy', broken down into combinations of sequential characters in a row of 2 or more.

Now, suppose I also have a dictionary of many words (but I will fill the example dictionary with random words):

Code:
dict = ['strringy', 'rangy', 'stare', 'guy', 'rain', 'string', 'gay', 'yag', 'eats', 'stringg']

What I want to do is cycle through dict with each group of words (or single word) in example[0] to example[-1].

I want to check if any strings in example match up with a word in dict. The catch is the words in example can include a vowel (but no extra consoants). The second catch is that for each group of strings in example, there must be a match in dict for all of the strings, or the match is invalid.

Expected output:

For example[0] the expected output for matches in dict would be:
strringy
example[1]:
Nothing--no match for the 'rr' part
example[2]:
stare, rangy
example[3]:
Nothing matches
example[4]:
stare, run, guy
stare, run, gay
# two distinct matches here. Notice yag doesn't work because of what I specified above.
example[5], example[6], example[7]:
No matches

Final output (ideally stored in a variable?)

Code:
final = [['stare', 'rangy'], ['stare', 'run', 'guy',], ['stare', 'run', 'gay']]

p "The matches were #{final}, and there #{final.size} total matching phrases!"

#=> "The matches were ['stare', 'rangy'], ['stare', 'run', 'guy',], ['stare', 'run', 'gay'], and there 3 total matching phrases!"


I want to write a loop or method that will print out all of the answers to this question at once, and print the answer for example[n] in the correct order. Ie, for example[2], do NOT print rangy, stare; this is not the correct order.

I started by taking dict, and using a regex to take out the vowels from each word in dict.

Code:
foo = dict with no vowels

Then match a word or words in foo with word or words in example.

I don't know if what I am asking is possible, but I wanted to reach out for help as I am working on this for fun as a side project. Any help is much appreciated. :thumbs: