use-revolution Digest, Vol 26, Issue 7
Marielle Lange
mlange at lexicall.org
Tue Dec 6 14:13:21 EST 2005
Dear all,
They have already been many replies to that post that I read only
now. But they all seemed very long to me. I used it as an excuse to
get back to transcript programming after a month away from any
computer. I propose an entry for the shortest solution. You have to
assume that the head and tails series are in a text field called
"headtails" and the script executed when pressing on a button.
The reason I post this, long after the original mail is because an
original solution could be used, thanks to regular expressions and
the possibility you have to define a letter or a text part to be
matched a specific number of times. This is captured with the (text)
{3} syntax. By replacing the repetition of letter with nothing, it is
possible to keep track of the number of matches for each nplets of
letters.
on mouseup
put empty into tresult; put field "headtails" into headtails
put replacetext(headtails, "[^THth]","") into headtails --
precaution to discard any non TH character
put toUpper(headtails) into headtails -- precaution
put "H" into coin[1]; put "T" into coin[2]
put "Head" into coinName[1]; put "Tail" into coinName[2]
put false into fstMatch
repeat with x = 5 down to 1 -- replace 5 with the
expected length of the longest match,
-- or
if that value is unknown and cannot be guessed, with the length of
the headtails string.
repeat with c = 1 to 2 -- flip between head
and tails.
put replacetext(headtails,"(" & coin[c] & "){" & x & "}", "")
into tresult
if tresult <> headtails then -- a match was found
put coinName[c] & " x " & x & " = " & ((length(headtails) -
length(tresult)) / x) & cr after tTable
put replacetext(headtails,"(" & coin[c] & "){" & x & "}",
"#") into headtails
put true into fstMatch -- this is so the the table only
starts with the maximum number of matches
else if fstMatch is true then
put coinName[c] & " x " & x & " = " & 0 & cr after tTable
end if
end repeat
end repeat
sort lines of tTable descending by item 1 of each
put tTable
end mouseup
I am now at Nov 2. Only a bit more of 150 digests to skim through...
Marielle
> 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
>
------------------------------------------------------------------------
--------
Marielle Lange (PhD), Psycholinguist
Alternative emails: mlange at blueyonder.co.uk, M.Lange at ed.ac.uk
Homepage
http://homepages.lexicall.org/mlange/
Easy access to lexical databases http://lexicall.org
Supporting Education Technologists http://
revolution.lexicall.org/wiki
More information about the use-livecode
mailing list