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/