[LRUG] Recursion diversion

Peter Vandenabeele peter at vandenabeele.com
Fri Oct 25 10:50:38 PDT 2013


On Fri, Oct 25, 2013 at 3:21 PM, Andrew Stewart
<boss at airbladesoftware.com>wrote:

> Hello El Rug,
>
> Given two strings a and b, I would like to find all the occasions where
> the letters of b appear in the same order in a.  The result should be an
> array of arrays where each inner array contains the indices of b's matches
> in a.
>
> a: 'hello world'
> b: 'e'
> result: [ [1] ]
>
> a: 'hello world'
> b: 'l'
> result: [ [2,3,9] ]
>
> a: 'hello world'
> b: 'el'
> result: [ [1,2], [1,3], [1,9] ]
>
> a: 'hello world'
> b: 'lo'
> result: [ [2,4], [2,7], [3,4], [3,7] ]
>
> a: 'hello world'
> b: 'lod'
> result: [ [2,4,10], [2,7,10], [3,4,10], [3,7,10] ]
>
> Hope that makes sense ;)
>
> I've been trying this for ages, with iteration and recursion, and I keep
> getting close...but not quite there.
>
> Any takers?
>
>
I implemented a (quite long) implementation with iteration. Seems to work.

I did change example 2 to result in:

  [ [2], [3], [9] ]

that seemed more logical to me (there are 3 results, each of length 1).

  https://gist.github.com/knaveofdiamonds/7155189#comment-936642

Had fun doing this :-)

Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20131025/9338d953/attachment.html>


More information about the Chat mailing list