counting runs
Cubist at aol.com
Cubist at aol.com
Tue Nov 1 05:28:00 EST 2005
sez j.tangen at unsw.edu.au:
>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).
Presuming the data is indeed a string of Ts and Hs, separated by commas,
something like this might do what you want...
local LongestRun = 60 # this value can be dialed up or down as desired
function FindRuns TheFlips
# TheFlips must be a comma-delimited series of Ts and Hs!
# if TheFlips is anything else, God knows what this handler will do to it
put TheFlips into DerTempT
put TheFlips into DetTempH
put "T" into RunsT
put "H" into RunsH
repeat
put RunsT into item (1 + the number of items in RunsT) of RunsT
put RunsH into item (1 + the number of items in RunsH) of RunsH
if (the number of items in RunsT => LongestRun) then exit repeat
end repeat
put ("," & (item 1 to LongestRun of RunsT) & ",") into RunsT
put ("," & (item 1 to LongestRun of RunsH) & ",") into RunsH
put true into TsLeft
put true into HsLeft
put "Record of Ts" into RezultT
put "Record of Hs" into RezultH
repeat with K1 = LongestRun down to 1 # or however many
# first, let's see if we need to worry about any further runs
put (offset ("T",DerTempT) > 0) into TsLeft
put (offset ("H",DerTempH) > 0) into HsLeft
if TsLeft then
# we still have some Ts
if offset (RunsT,DerTempT) > 0 then
# hey, we got at least one run of this particular length!
put the length of DerTempT into DerLength
replace RunsT with "," in DerTempT
# RunsT is a string of length (2n+1), where n is the number of Ts in
it.
# if we replace all instances of RunsT with single commas, that
shortens
# DerTempT by (2n) characters per instance that got replaced.
therefore...
put (DerLength - the length of DerTempT) div (2 * K1) into NumOfRuns
else # nope, no more Ts left, hence no runs of however-many Ts
put 0 into NumOfRuns
end if
put ("Heads x" & K1 && "=" && NumOfRuns) & return before RezultT
end if
# now do to the Hs what we did to the Ts
if HsLeft then
if offset (RunsH,DerTempH) > 0 then
put the length of DerTempH into DerLength
replace RunsH with "," in DerTempH
put (DerLength - the length of DerTempH) div (2 * K1) into NumOfRuns
else
put 0 into NumOfRuns
end if
put ("Tails x" & K1 && "=" && NumOfRuns) & return before RezultH
end if
# now adjust RunsT and RunsH for the next time around the repeat loop
delete char 1 to 2 of RunsT
delete char 1 to 2 of RunsH
end repeat
# clean up RezultT and RezultH -- nuke the crap in the final line of each
delete line -1 of RezultT
delete line -1 of RezultH
return RezultT & return & "=======" & return & RezultH
end FindRuns
Hope this helps...
More information about the use-livecode
mailing list