Heads & Tails

Jim Hurley jhurley at infostations.com
Fri Dec 9 10:56:12 EST 2005


>
>Message: 5
>Date: Fri, 9 Dec 2005 04:35:10 -0700
>From: Mark Greenberg <markgreenberg at cox.net>
>Subject: Heads & Tails
>To: Runtime Revolution <use-revolution at lists.runrev.com>
>Message-ID: <19B81045-AC13-42EF-B62F-ED8C42018268 at cox.net>
>Content-Type: text/plain;	charset=US-ASCII;	delsp=yes; 
>	format=flowed
>
>Here's another solution for the problem of finding the runs of heads 
>in a series of coin flips:
>
>Function TheRuns Data --in the form of tththhhhtthhth...
>    Local P1, P2=1
>    Repeat with i = 1 to 12
>      Put True into M;Put Data into D;Put 0 into Ct
>      Repeat until M is False
>        Put MatchChunk (D,"(?:^|t)(h{" & i & "})(?:t|$)",P1,P2) into M
>        If M then Add 1 to Ct
>        Delete Char 1 to P2 of D
>      end Repeat
>      Put Ct & " runs of " & i & Return after Report
>    end Repeat
>    Return Report
>end TheRuns


Mark and Marielle,

The many ways of skinning a cat:

on mouseUp
   put field 1 into tText
   put 0 into tHeads
   repeat for each char tChar in tText
     if tChar is "h" then add 1 to tHeads
   end repeat
   put the number of chars in tText - tHeads into tTails
   put tHeads, tTails into msg box
end mouseUp

Given field 1 of the form hhhththhthhhth

Jim







More information about the Use-livecode mailing list