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: [mg22447] Re: Detect pairs, tripples in a list
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 8 Mar 2000 02:21:47 -0500 (EST)
  • References: <89qe2g$a4l@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hans,

1. If the ones occur in data in runs of 1, 2 or 3 and not in any longer runs
then

Append[data, 3] //. {
      {1, 1, 1, b___} -> {b, 0, 0, 1},
      {1, 1, b___} -> {b, 0, 1},
      {1, b___} -> {b, 1},
      {0, b___} -> {b, 0}
      } // Rest

(The appended 3 comes to the first position to terminate the replacement.
Notice that pattern matching is concerned with only the first three entries,
at most. This makes it much faster than

data//.{{a___,1,1,1,b___} ->{a,0,0,1,b},
        {a___,1,1,b___}->{a,0,1,b}})

2. Otherwise we have to decide what to make of  lists like {1,1,1,1} -
should it become {0,0,1,1}, or should we allow backtracking or a second run,
so that it becomes {0,0,0,1}?
The following takes the latter view.

Append[data, 3] //. {
      {1, 1, 1, b___} -> {b, 0, 0, 1},
      {1, 1, b___} -> {b, 0, 1},
      {1, b___} -> {b, 1},
      {b___, 1, 1} -> {b, 0, 1}, (*extra rule*)
      {0, b___} -> {b, 0}
      } // Rest

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Hans Friedrich Steffani" <hans.steffani at e-technik.tu-chemnitz.de> wrote in
message news:89qe2g$a4l at smc.vnet.net...
> 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/
>



  • Prev by Date: Re: Trouble with backspace under Linux/i386
  • Next by Date: equation is too complex?
  • Previous by thread: Re: Detect pairs, tripples in a list
  • Next by thread: Re: Detect pairs, tripples in a list