MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Detect pairs, tripples in a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22491] Re: [mg22442] Detect pairs, tripples in a list
  • From: "Tomas Garza" <tgarza at mail.internet.com.mx>
  • Date: Wed, 8 Mar 2000 02:22:32 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hans Friedrich Steffani [hans.steffani at e-technik.tu-chemnitz.de] wrote:

> I have a list with many 0s and some 1s. Now I want to detect pairs
> and tripples of 1s with no 0 between to substitute them by a single 1
> and the apropriate number of 0. How can this be done?

Am I right in assuming that you want to detect not only pairs and triples,
but all n-tuples, and substitute each of these as stated? If that is the
case, then you may define

In[1]:=
f[x_, n_] := x /. Table[1, {n}] -> Prepend[Table[0, {n - 1}], 1]

and then use

Flatten[Fold[f, Split[myList], Range[Length[myList]]]]

Example:

In[2]:=
myList = Table[Random[Integer, {1, 0}], {20}]
Out[2]=
{0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1}

In[3]:=
Flatten[Fold[f, Split[myList], Range[Length[myList]]]]
Out[3]=
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0}

Tomas Garza
Mexico City



  • Prev by Date: Re: Detect pairs, tripples in a list
  • Next by Date: Re: Mathlink speed
  • Previous by thread: Re: Detect pairs, tripples in a list
  • Next by thread: Re: Detect pairs, tripples in a list