counting runs
Alex Tweedly
alex at tweedly.net
Mon Oct 31 19:46:58 EST 2005
Jason Tangen wrote:
> Hello,
>
> I'm trying to solve a seemingly simple problem and I could use some
> help. A coin flip will come out "Heads" or "Tails" and will produce,
> for example, the following series for 20 flips:
>
> T,T,H,T,T,H,H,H,T,T,H,T,H,H,H,T,H,T,H,T
>
> I need to count up the number events for a particular run. For example,
>
> Heads x 3 = 2
> Heads x 2 = 0
> Heads x 1 = 4
> Tails x 1 = 4
> Tails x 2 = 3
> Tails x 3 = 0
>
> I need to account for runs up to 60 (rather than 3 above) for
> hundreds of flips (rather than 20 above).
>
> I've been using a very clumsy multiple if-then solution to date, but
> this quickly becomes difficult with many runs.
>
> Can someone point me in the right direction?
something like
> local myVar, MyData, MyArray, I, tRunLen, tRunType
>
> put fld "Field" into MyData
> put 0 into tRunLen
> put "" into tRunType
>
> repeat for each item I in myData
> if I <> tRunType then
> if tRunType <> "" then
> add 1 to MyArray[tRunType, tRunLen]
> end if
> put I into tRunType
> put 1 into tRunLen
> else
> add 1 to tRunLen
> end if
> end repeat
> if tRunLen > 0 then
> add 1 to MyArray[tRunType, tRunLen]
> end if
>
> put the keys of MyArray into temp
> sort temp
> put temp into msg
--
Alex Tweedly http://www.tweedly.net
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.361 / Virus Database: 267.12.6/151 - Release Date: 28/10/2005
More information about the use-livecode
mailing list