#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Help with finding array inside of an array for a simple algorithm


    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.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1945
    Not exactly sure which part(s?) you want help with, but it looks like you try to use the example array as one-dimensional, when you have it created as two-dimensional.

    Using example[0] will give you the array object ['strrngy'], while example[0][0] will give you the value 'strrngy'.

    Similar, to get the value 'rr' you will have to use example[1][1].

    Before you try comparing arrays, I will suggest you make sure you get the correct/expected value.
    Do you know how to iterate through multi-dimensional array? What about the each function? If not, I suggest you try to look this up in first place.

    If there are something I have misunderstood, please clarify.

IMN logo majestic logo threadwatch logo seochat tools logo