You cannot do it in one shot. You have to come up with the perfect
algorithm. Although one can come up with a piece a code for you to start
but I'll leave a few hints so you get to practice, which is what you
definitely want. Assuming irb, start by doing this:

1. Construct an array out of each character
>> find = "stringy".each_char.map { |x| p x }
=> ["s", "t", "r", "i", "n", "g", "y"]

2. Make a consecutive array for each of the characters of "find".
>> continuous_array = (0...find.length).to_a.each { |x| p find[0..x] }
=> ["s"]
["s", "t"]
["s", "t", "r"]
["s", "t", "r", "i"]
["s", "t", "r", "i", "n"]
["s", "t", "r", "i", "n", "g"]
["s", "t", "r", "i", "n", "g", "y"]

3. Similarly, do it for the reverse order:
> reverse_continuous_array = (-find.length..-1).to_a.reverse.each { |x| p
find[x..-1] }
=> ["y"]
["g", "y"]
["n", "g", "y"]
["i", "n", "g", "y"]
["r", "i", "n", "g", "y"]
["t", "r", "i", "n", "g", "y"]
["s", "t", "r", "i", "n", "g", "y"]

Now, you have got your building blocks. Now it is up to you to what you
want to do with them (mainly append them into a big array of arrays and
then loop through them using "loop" or whatever way you find easy and
search through your ex_array, find the matches with Array#include? and
construct your results).


On Thu, Dec 19, 2013 at 12:13 AM, Jim Whittaker <lists / ruby-forum.com>wrote:

> I'm working on making a word game with my friend, but ran into this
> problem I can't solve.
>
> So say I have the string:
>
> example = "stringy"
>
> And the array:
>
> ex_array = ['estrange', 'gingerly', 'stop', 'geek', 'dry', 'rad', 'ear',
> 'wrinkle', 'cringy', 'guy', 'stringwingy']
>
> Now, I want to iterate each letter of "stringy" through this array.....
> s-t-r-i-n-g-y....
>
> The loop should return the words from the array that meet the following
> conditions:
>
> 1) Contain more than 1 consonant (assuming y is a consonant)
>
> 2) Returns a match to any consecutive group of character in the
> "stringy" (ex. 'st', 'str', 'stri'..etc)
>
> 3) Displays the groups of phrases that can be matched from inside of the
> array, leaving no letter in the "string" unaccounted for.
>
> 4) Displays a count of total number of matches at the bottom.
>
> Desired output from the example above: # text to the right of # is not
> to be printed by the loop
>
> estrange gingerly # str ingy MATCH
> stop cringy # st ringy MATCH
> stop wrinkle guy # st rin gy MATCH
> stringywingy # stringy MATCH
> Total: 4 matches
>
> ---
> # geek, dry, rad, ear are not a match for any consecutive (min 2)
> letters in 'stringy'
> # 'ear' has too many consonants
> # estrange cringy is not a match because the 'r' overlaps
>
>
> Is there a way to get this done with nested for loops, or any other more
> efficient way?
>
> Thanks ;)
>
> --
> Posted via http://www.ruby-forum.com/.
>