Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Group and Replace itens sequence in a list
Replies: 10   Last Post: Aug 29, 2012 1:14 AM

 Messages: [ Previous | Next ]
 Simons, F.H. Posts: 107 Registered: 12/7/04
Re: Group and Replace itens sequence in a list
Posted: Aug 25, 2012 4:26 AM

Solutions with pattern matching can be very elegant, such as Bob
Hanlon's solution, but also tend to be very slow when the problem is a
little bit larger.

Here is a solution by finding the position of the triple {a,b,a} and
then taking the parts of the list outside these triples.

g[lst_] := With[{limits=Partition[Flatten[{1,
Position[Partition[lst,3,1], {a,b,a}] /.
{n_Integer}->{n-1,n+3},Length[lst]}],2]}, Take[lst, #]& /@ limits]

In the following commands f is Bob's function:

In[4]:= lst={1,2,3,a,b,a,4,5,6,a,b,c,7,8,9,a,b,a,10,11,12} ;
z1=f[lst]; // Timing
z2=g[lst]; // Timing
z1===z2
Out[5]= {0.,Null}
Out[6]= {0.,Null}
Out[7]= True

Both functions are fast enough. Now we make the list 500 times longer:

In[12]:= lstt=Flatten[Table[lst, {500}],1];
z1=f[lstt]; // Timing
z2=g[lstt]; // Timing
z1===z2
Out[13]= {5.007632,Null}
Out[14]= {0.015600,Null}
Out[15]= True

Fred Simons
Eindhoven University of Technology

Op 24-8-2012 11:04, Bob Hanlon schreef:
> Clear[f]
>
> f[list_?VectorQ] := Cases[
> (list //. {s___, a, b, a, r___} -> {{s}, {r}}) /.
> {} ->
> Sequence[], _?VectorQ, Infinity]
>
> f[{1, 2, 3, a, b, a, 4, 5, 6, a, b, c, 7, 8, 9, a, b, a, 10, 11, 12}]
>
> {{1, 2, 3}, {4, 5, 6, a, b, c, 7, 8, 9}, {10, 11, 12}}
>
> f[{a, b, a, 1, 2, 3, a, b, a, 4, 5, 6, a, b, c, 7, 8, 9, a, b, a, 10,
> 11, 12}]
>
> {{1, 2, 3}, {4, 5, 6, a, b, c, 7, 8, 9}, {10, 11, 12}}
>
> f[{1, 2, 3, a, b, a, 4, 5, 6, a, b, c, 7, 8, 9, a, b, a, 10, 11, 12,
> a, b, a}]
>
> {{1, 2, 3}, {4, 5, 6, a, b, c, 7, 8, 9}, {10, 11, 12}}
>
> f[{a, b, a, 1, 2, 3, a, b, a, 4, 5, 6, a, b, c, 7, 8, 9, a, b, a, 10,
> 11, 12, a, b, a}]
>
> {{1, 2, 3}, {4, 5, 6, a, b, c, 7, 8, 9}, {10, 11, 12}}
>
>
> Bob Hanlon
>
>
> On Thu, Aug 23, 2012 at 2:52 AM, Murta <rodrigomurtax@gmail.com> wrote:

>> Hi All
>>
>> I have a simple problem that is:
>>
>> l={1,2,3,a,b,a,4,5,6,a,b,c,7,8,9,a,b,a,10,11,12}
>>
>> I want to replace all a,b,a sequence by X to get:
>>
>> l={1,2,3,X,4,5,6,a,b,7,8,9,X,10,11,12}
>>
>> Then I want to group it by X intervals as
>> l={{1,2,3},{4,5,6,a,b,7,8,9},{10,11,12}}
>>
>> If I don't need to put the intermediate X, even better!
>> I think the with pattern, RaplaceAll and DeleteCases I can do It. Some clue?
>> Tks
>> Murta
>>
>>
>>

>
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2012.0.2197 / Virusdatabase: 2437/5219 - datum van uitgifte: 08/23/12
>
>
>

Date Subject Author
8/23/12 Ulrich Arndt
8/23/12 Ulrich Arndt
8/24/12 Bob Hanlon
8/25/12 Murta
8/25/12 Simons, F.H.
8/26/12 Murta
8/28/12 Ulrich Arndt
8/29/12 Dana DeLouis