Re: Max Consecutive Occurrence.
- To: mathgroup at smc.vnet.net
- Subject: [mg47664] Re: [mg47653] Max Consecutive Occurrence.
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 21 Apr 2004 05:23:03 -0400 (EDT)
- References: <200404200719.DAA27856@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 20 Apr 2004, at 16:19, Robert G. Wilson v wrote:
>
> Mathematica had many programmed functions which are not referenced in
> the
> help index. Under '?Position -> Further Examples' you will find two
> such
> functions, 'ConsecutiveOccurrences1' and 'ConsecutiveOccurrences2.' I
> first wish that Mathematica would index these.
>
> But the reason for writing is that I have a need for a function named
> 'MaxConsecutiveOccurrence' which would return the maximum consecutive
> occurrence of x in a list.
>
> Thank you in advance.
>
>
> Bob.
>
>
>
What do you mean by :
> would return the maximum consecutive
> occurrence of x in a list
?
The length of such a occurance? The position of, the elements of such
an occurance? The function below will give you both as a pair, {maximum
number of consecutive occurences, position of the first element of the
maximal sequence of consecutive occurences}. If you do not need to know
oen of these it is easy to make the necessary modificantion.
f[l_, a_] := Module[{A =
Split[l], B}, B = Last[Sort[Cases[A, x : {a ..} :> {
Length[x], Position[A, x][[1,
1]]}]]]; {First[B], Length[Flatten[Take[A, Last[B] - 1]]] +
1}]
For example, let
l={a,a,b,c,d,b,b,a,a,a,f,a};
then:
f[l,a]
{3,8}
f[l,b]
{2,6}
Andrzej Kozlowski
Chiba, Japan
http://www.mimuw.edu.pl/~akoz/
- References:
- Max Consecutive Occurrence.
- From: "Robert G. Wilson v" <rgwv@rgwv.com>
- Max Consecutive Occurrence.