[Tutor] Finding the "streaks" in heads/tails list

Alan Gauld alan.gauld at btinternet.com
Thu Oct 2 02:27:39 CEST 2008

"Kent Johnson" <kent37 at tds.net> wrote

>> What I want to do, is find out the largest "streak" of digits. In 
>> the
>> above example, the streak would be 5, because there are 5 tails 
>> flips in
>> a row.

> I would loop through the list with a for loop, keeping track of the
> last value seen and the current count. If the current value is the
> same as the last, increment the count; if it is different, reset the
> count.

You need to store the count before resetting it since you want
to know the largest value of count over the list. Or at least keep a
separate max variable that you update if count > max.

But as Kent also said the easiest way is probably to just track the
runs and their count as the data is generated rather than waiting
till the end and post-processing the results.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list