Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a tricky ruby problem--array of array matching
#1
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:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting Started With Ruby eax 7 5,027 12-13-2012, 10:03 AM
Last Post: L-Carnitine
  [Tut] How to install Ruby on Mac, Windows and Linux Jordan L. 13 8,429 12-13-2012, 07:49 AM
Last Post: L-Carnitine
  Ruby eBook sockatobi 7 3,058 12-13-2012, 07:48 AM
Last Post: L-Carnitine
  what is ruby on rails DaUB3R 3 2,030 11-01-2012, 04:20 AM
Last Post: johnnymward
  How to re-do an array into arrays timfox123 0 1,062 04-05-2012, 08:01 AM
Last Post: timfox123

Forum Jump:


Users browsing this thread: 1 Guest(s)