Competition #1: Reversing a string
Posted: Thu Dec 05, 2013 8:22 pm
..........
Questions and answers about the LiveCode platform.
https://forums.livecode.com/
Code: Select all
repeat with j=length(s) down to 1
Code: Select all
put len(s) into N
repeat with j=N down to 1
Code: Select all
function reversed pString
local tReversedString
repeat with tChar = the length of pString down to 1
put char tChar of pString after tReversedString
end repeat
return tReversedString
end reversed
Code: Select all
function reverseString theString
put the number of chars of theString into charCount
repeat for each char thischar in theString
put char charCount of theString after backwardsString
subtract 1 from charCount
end repeat
return backwardsString
end reverseString
Code: Select all
function reversed pString
put 1 into tCounter
repeat for each char tChar in pString
put tChar into tArray[tCounter]
add 1 to tCounter
end repeat
put the keys of tArray into tKeys
sort lines of tKeys numeric descending
repeat for each line tLine in tKeys
put tArray[tLine] after reversedtext
end repeat
return reversedtext
end reversed
The repeat loop is an inherent part of the function.[-hh] wrote:You are optimizing the testing procedure, not the function of interest itself.
Whether s originated from a file read into memory, or is loaded from the contents of a field, or is created ad hoc via script, it would still be a variable in memory. At the point the function is called it shouldn't matter where it originated.[-hh] wrote:is it possible, that you don't have the same conditions for the test of the functions? For example with reading from disk or memory.
Code: Select all
function reverseString pString
local tString
repeat with tIndex = number of chars in pString down to 1
put char tIndex of pString after tString
end repeat
return tString
end reverseString
well, I can sleep well nevertheless[-hh] wrote:the bad news:
With my settings your function is about 8 times slower than the LCReverse.
Very kind, but thanks, no Schnaps for me.[-hh] wrote:The good news:
If you like Grappa (or do you prefer Obstler?) I'll send you a bottle.