NPR puzzle

Geoff Canyon gcanyon at inspiredlogic.com
Fri Jul 22 11:04:53 EDT 2005


The engine optimizes the tests in conditionals. So in this statement:

if a=3 and b=2 then...

b never gets evaluated if a is not 3. I rewrote Dick's very nice code  
using this principle, to get rid of the nested if statements.  
Interestingly, my code slowed down a couple milliseconds. Just from  
13 to 15, but I wonder why that might be. Maybe the engine goes to  
the trouble of getting the substrings before doing the tests.

Here's the code:

   put the milliseconds into tMilliseconds
   repeat for each line tSymbol in field 2
     put "true" into S[tSymbol]
   end repeat
   repeat for each word W in field 1
     if S[char 1 to 2 of W] and \
         S[char 3 to 4 of W] and \
         S[char 5 to 6 of W] and \
         S[char 7 to 8 of W] and \
         S[char 9 to 10 of W] then \
         put W & return after tHits
   end repeat
   put the milliseconds - tMilliseconds into tElapsedMilliseconds
   put number of lines in tHits && "hits in" \
       && tElapsedMilliseconds && "milliseconds" \
       & return & return & tHits

On Jul 22, 2005, at 6:09 AM, Jim Hurley wrote:

>>
>> Message: 8
>> Date: Thu, 21 Jul 2005 14:19:37 -0700
>> From: Dick Kriesel <dick.kriesel at mail.com>
>> Subject: Re: NPR puzzle
>> To: How to use Revolution <use-revolution at lists.runrev.com>
>> Message-ID: <BF055F79.F1B9%dick.kriesel at mail.com>
>> Content-Type: text/plain;    charset="US-ASCII"
>>
>> Here's a solution that takes 10 milliseconds on a 1GHz PowerBook G4.
>>
>> The main reason for the speed improvement over previous solutions  
>> is that
>> array look-ups are typically faster than string searches.
>>
>>   put the milliseconds into tMilliseconds
>>   repeat for each line tSymbol in field 2
>>     put "true" into tSymbolArray[tSymbol]
>>   end repeat
>>   repeat for each word tWord in field 1
>>     if tSymbolArray[char 1 to 2 of tWord] then
>>       if tSymbolArray[char 3 to 4 of tWord] then
>>         if tSymbolArray[char 5 to 6 of tWord] then
>>           if tSymbolArray[char 7 to 8 of tWord] then
>>             if tSymbolArray[char 9 to 10 of tWord] then
>>               put tWord & return after tHits
>>             end if
>>           end if
>>         end if
>>       end if
>>     end if
>>   end repeat
>>   put the milliseconds - tMilliseconds \
>>       into tElapsedMilliseconds
>>   put number of lines in tHits && "hits in" \
>>       && tElapsedMilliseconds && "milliseconds" \
>>       & return & return & tHits
>>
>> -- Dick
>>
>
> Dick,
>
> Dazzling!
>
> Jim
> _______________________________________________
> 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