The Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bob Hanlon

Posts: 906
Registered: 10/29/11
Re: Group and Replace itens sequence in a list
Posted: Aug 24, 2012 5:07 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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
>
>
>





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.