NPR puzzle

Alex Tweedly alex at tweedly.net
Wed Jul 20 14:55:38 EDT 2005


Ro Nagey wrote:

> Here's my humble submission:
>
> on doIt
>   put the ticks into startTime
>   put fld "element" into gorp
>   repeat for each line l in fld "words"
>     put "Yes" into myAnswer
>     put  l  into myWord
>     put myWord into daWord
>     repeat 5 times
>       put char 1 to 2 of daWord into anElement
>       delete char 1 to 2 of daWord
>       if anElement is not in gorp then
>         put "No" into myAnswer
>         exit repeat
>       end if
>     end repeat
>     if myAnswer contains "Yes" then put myWord & return after  
> finalAnswer
>   end repeat
>   put the ticks-startTime into totalTicks
>   answer totalTicks && finalAnswer && the number of lines in  finalAnswer
> end doIt
>
> Did it in 2 ticks and found 23 words :)
>
Ro,
Just so we have comparable times - I converted it to report millisecs, 
and on my machine it takes 29 msec.

I made a couple of tweaks:
1  Changed
repeat for each line l in fld "words"
    ...
    put l into myWord
to  simply
   repeat for each line myWord in fld "words"
since l was not used anywhere else, and myWord was not changed.

2. moved the "delete char 1 to 2 of daWord" to after the if test (no 
need to delete them if you are about to exit the loop).

Those improved it to 20 msecs (which I think is the limit of resolution 
on my machine - the original was sometimes 28, sometimes 29, sometimes 
31 - now it is always 20.



-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.9.0/50 - Release Date: 16/07/2005




More information about the use-livecode mailing list