Re: Fast List-Selection
- To: mathgroup at smc.vnet.net
- Subject: [mg19915] Re: [mg19880] Fast List-Selection
- From: "Arnold Knopfmacher" <arnoldk at cam.wits.ac.za>
- Date: Tue, 21 Sep 1999 02:22:49 -0400
- Organization: MS, University of the Witwatersrand
- Sender: owner-wri-mathgroup at wolfram.com
Hi, this is faster than my last effort (also given below) dif[s_]:=Drop[s,1]-Drop[s,-1]; nConsecD2[l_,n_]:=With[{pat=Table[0,{n-1}]}, Flatten[Position[Partition[dif[l],n-1,1],{x__} /;{x}==pat]]] s=Table[Random[Integer,{1,4}],{50000}]; nConsecD2[s,7]//Timing {3.35 Second,{37896,40088,40089,40703,42736,42737,42738,46784}} nConsec[s,7]//Timing {8.24 Second,{37896,40088,40089,40703,42736,42737,42738,46784}} >Here is one approach to finding starting positions of n consecutive identical >elements: nConsec[l_,n_]:= Flatten[Position[Partition[l,n,1],{x__} /;Length[Union[{x}]]==1]] s={2,3,3,3,3,3,3,3,3,3,4,4,5,5,5,5,5,5,5,4} nConsec[s,7] {2,3,4,13} Arnold Knopfmacher > Date: Sun, 19 Sep 1999 18:47:32 -0400 > From: Hans Havermann <haver at total.net> To: mathgroup at smc.vnet.net > To: mathgroup at smc.vnet.net > Subject: [mg19915] [mg19880] Fast List-Selection > I have a list 's' composed of a large number of (small) integers. I wish to > search this list for instances of 7 consecutive, identical elements. > > My approach is: > > Do[If[Count[t = Take[s, {i, i + 6}], t[[1]]] == 7, > Print[i]], {i, 1, Length[s] - 6}] > > Can anyone think of a *faster* way of doing this? > > > > Arnold Knopfmacher Dept of Computational and Applied Maths Witwatersrand University Johannesburg 2050 South Africa http://www.wits.ac.za/science/number_theory/arnold.htm Fax: 2711-4039317 Phone: 2711- 7163353 email: arnoldk at gauss.cam.wits.ac.za