MathGroup Archive 2006

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

Search the Archive

Re: question about Split[...]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64547] Re: [mg64529] question about Split[...]
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 22 Feb 2006 05:58:30 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

data=Table[Random[Integer],{20}]

{1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0}

k=3;

To limit any run to at most k elements

Split[data]//.x_?(Length[#]>k && Union[#]=={First[#]}&):>
    Sequence[Table[First[x],{k}],Table[First[x],{Length[x]-k}]]

{{1},{0,0,0},{0,0,0},{0,0},{1,1,1},{1,1,1},{0},{1,1},{0,0}}

p=1;

To limit only runs of p to at most k elements

Split[data]//.x_?(Length[#]>k && Union[#]=={p}&):>
    Sequence[Table[p,{k}],Table[p,{Length[x]-k}]]

{{1},{0,0,0,0,0,0,0,0},{1,1,1},{1,1,1},{0},{1,1},{0,0}}


Bob Hanlon

> 
> From: "Capet Arthur" <Arthur.Capet at student.ulg.ac.be>
To: mathgroup at smc.vnet.net
> Subject: [mg64547] [mg64529] question about Split[...]
> 
> the question...
> 
> How can i restrict the maximum length of the sublist generated by the
> function Split[list, test] ?
> 
> 
> In details...
> 
>   i need to subdivise a given list into sublists consisting into a given
> form (here it's p), repeated AT MAXIMUM k times, and then the
> just-following  element in the initial list :
> 
> something like {p,... k times...,p, next})
> 
> i've found -- Split [ll, #1 == p &] -- useful but don't know how to limit 
> the length of the generated sublists (to k+1)
> 
> 
>  thanks a lot
> 
> 
> 
> Arthur Capet, ULG, Belgium
> 
> 


  • Prev by Date: Re: ListSphericalDensityPlot[] ??
  • Next by Date: Re: Code to translate to negative base...
  • Previous by thread: Re: question about Split[...]
  • Next by thread: ListSphericalDensityPlot[] ??