counting runs
Jim Ault
JimAultWins at yahoo.com
Tue Nov 1 02:20:20 EST 2005
On 10/31/05 4:22 PM, "Jason Tangen" <j.tangen at unsw.edu.au> 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.
<long reply>
This example is a rather involved routine, but it shows several things that
will help you analyze the flipping results. (Sorry about the pun, but could
not resist).
<slappinghand force = ³strong²>6 times</slappinghand>
This solution does not use Arrays. It is 142 lines (not going for shortest
solution here)
It very quickly generates a report of statistics, and 6 graphic
representations
It could be worth your time to play with it, then again you might like the
arrays better.
This code includes a breakpoint at the end so that it will automatically
stop and you can study the variables that it builds.
Hope this is helpful to you and others.
Jim Ault
Las Vegas
----------- start copying here ------------------
on headsUpReporting --10.31.05
--prepares (1) final table of statistics
-- + (6) graphic representations of the results
--
--PAUSE at the last breakpoint to see all the variables
-- then decide how you want to present the results
--
--the commas are redundant for most of the work,
-- but, conveniently, we will use them later for graphics
--
--since working with arrays may be confusing to you and others
-- I offer this solution which DOES NOT use arrays, but rather
-- repeat loops, words, and lines
-- (sorry about the rogue format of the variables.. old, old habit)
--variation of Mark Smith's earlier reply
repeat 10000
--take heart, couple of seconds and it is all done, Rev is fast
put any item of "H,T" after originalStrr -- just to generate a sample
list
put comma after originalStrr --we want the commas here
end repeat
if last char of originalStrr is comma then delete last char of
originalStrr
--
-- this is a cross-tab technique vs an array accumulator
--
put originalStrr into headsOnly
put originalStrr into tailsOnly
replace comma with empty in headsOnly
replace comma with empty in tailsOnly
put the number of characters in (headsOnly) into flipsTotal --a simple
tally
replace "T" with space in headsOnly --strip t's (hey.. I'm in Vegas)
replace "H" with space in tailsOnly
put the number of words in headsOnly into maxx
repeat with x = 1 to maxx
get number of chars in word x of headsOnly
add 1 to item 2 of line (it) of finalTable
put 0 into item 3 of line (it) of finalTable
put (it) into item 1 of line (it) of finalTable --1st col of table
end repeat
put maxx into numRunsHeads --a simple tally
put the number of words in tailsOnly into maxx
repeat with x = 1 to maxx
get number of chars in word x of tailsOnly
add 1 to item 3 of line (it) of finalTable
put (it) into item 1 of line (it) of finalTable
end repeat
put maxx into numRunsTails --a simple talley
get headsOnly
replace space with empty in it
put the number of chars in (it) into numHeads --a simple tally
get tailsOnly
replace space with empty in it
put the number of chars in (it) into numTails --a simple tally
put 0 into maxHRun
put 0 into maxTRun
put the number of lines in finalTable into maxx
--fill in the table so it is easier to read
repeat with x = 1 to maxx
if item 1 of line x of finalTable < 1 then put x into item 1 of line x
of finalTable
if item 2 of line x of finalTable < 1 then put 0 into item 2 of line x
of finalTable
if item 3 of line x of finalTable < 1 then put 0 into item 3 of line x
of finalTable
end repeat
--get the longest run of heads & tails for the stats
repeat with x = maxx down to 1
if maxHRun < 1 then
if item 2 of line x of finalTable > 0 then
put item 1 of line x of finalTable into maxHRun
end if
end if
if maxTRun < 1 then
if item 3 of line x of finalTable > 0 then
put item 1 of line x of finalTable into maxTRun
end if
end if
--next test to see if both the max lengths have been found
if maxHRun > 0 and maxTRun > 0 then exit repeat
end repeat
--build the report
put "Report of " & flipsTotal & " Coin Flips" into finalReport
put " on " & the short date & " at " & the short time after finalReport
put return & "Total Heads = " & numHeads & " Num Runs = " & numRunsHeads
after finalReport
put return & "Total Tails = " & numTails & " Num Runs = " & numRunsTails
after finalReport
put return & "Max length Heads = " & maxHRun & " Avg Run = " &
numHeads/numRunsHeads after finalReport
put return & "Max length Tails = " & maxTRun & " Avg Run = " &
numTails/numRunsTails after finalReport
put return & return & "Len, Heads, Tails Runs" & return & finalTable after
finalReport
replace comma with space&space in finalReport
--visual ONE
put headsOnly into graphHeadsInSequence
put tailsOnly into graphTailsInSequence
replace space with return in graphHeadsInSequence
replace space with return in graphTailsInSequence
put graphHeadsInSequence into graphHeadsByLength
sort graphHeadsByLength descending
put graphTailsInSequence into graphTailsByLength
sort graphTailsByLength descending
--visual TWO
put originalStrr into graphBothInSequence
replace "H,T" with ("H" & return & "T") in graphBothInSequence
replace "T,H" with ("T" & return & "H") in graphBothInSequence
replace comma with empty in graphBothInSequence
--visual THREE
put graphBothInSequence into graphAsTwoGroups
sort lines graphAsTwoGroups descending
get ""
--------------- output ---------------
--make them easy to find in variable watcher
put finalReport into a_finalReport
put graphHeadsInSequence into a_graphHeadsInSequence
put graphTailsInSequence into a_graphTailsInSequence
put graphHeadsByLength into a_graphHeadsByLength
put graphTailsByLength into a_graphTailsByLength
put graphBothInSequence into a_graphBothInSequence
put graphAsTwoGroups into a_graphAsTwoGroupsByLength
--
--now view the following in variable watcher
-- a_finalReport
--
--visual ONE scroll to view
-- a_graphHeadsInSequence, a_graphHeadsByLength
-- a_graphTailsInSequence, a_graphTailsByLength
--
--visual TWO scroll to view
-- a_graphBothInSequence
--
--visual THREE scroll to view
-- a_graphAsTwoGroupsByLength
------------------------------------------
breakpoint -- PAUSE HERE and read the section above
end headsUpReporting
----------- stop copying here ------------------
On 10/31/05 4:22 PM, "Jason Tangen" <j.tangen at unsw.edu.au> 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?
>
> Cheers,
> Jason
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
More information about the use-livecode
mailing list