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: [mg22461] Re: [mg22442] Detect pairs, tripples in a list
  • From: "David Park" <djmp at earthlink.net>
  • Date: Wed, 8 Mar 2000 02:21:58 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>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?
>
>Hans Friedrich Steffani
>
>--
>Hans Friedrich Steffani
>Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz
>mailto:hans.steffani at e-technik.tu-chemnitz.de
>http://www.tu-chemnitz.de/~hfst/
>


Do you want the rule extended to any number of 1's in a row? If that is the case,
then the following routine is reasonably efficient. When you type this in
StandardForm, you can omit the parentheses around the 1 but be sure to leave a space
between the 1 and the "..".

NewData0[data_] :=
 Flatten[Split[data] /. a:{1, (1)..} :>
  {1, Sequence @@ Table[0, {Length[a] - 1}]}]

data = Table[Random[Integer, {0, 1}], {50}]
{1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1}

NewData0[data]
{1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}

On a 166 MHz Pentium:

data = Table[Random[Integer, {0, 1}], {50000}];
NewData0[data]; // Timing
{2.58 Second, Null}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/



  • Prev by Date: Re: Trouble with backspace under Linux/i386
  • Next by Date: Re: EllipticE, limit x->1 ???
  • Previous by thread: Re: Detect pairs, tripples in a list
  • Next by thread: Re: Detect pairs, tripples in a list