function for greatest object in list less than or equal to a value
Peter TB Brett
peter.brett at livecode.com
Mon Oct 12 04:55:17 EDT 2015
On 12/10/2015 03:26, Peter M. Brigham wrote:
> function getMaxLessThan tList,maxVal
> repeat for each item i in tList
> if i < maxVal then put i & comma after outList
> end repeat
> return max(item 1 to -1 of outList)
> end getMaxLessThan
This should be slightly faster, because it only loops over the items once:
function getMaxLessThan pList, pLimit
local tMaxFound, tItem
put empty into tMaxFound
repeat for each item tItem in tList
if tItem < pLimit and \
(tItem > tMaxFound or tMaxFound is empty) then
put tItem into tMaxFound
end if
end repeat
return tMaxFound
end getMaxLessThan
The "sort"-based solution will be much less efficient for long input
lists because sorting is O(N*log(N)), whereas Peter Brigham's and my
solutions are O(N).
Peter
--
Dr Peter Brett <peter.brett at livecode.com>
LiveCode Open Source Team
LiveCode on reddit: https://reddit.com/r/livecode
More information about the use-livecode
mailing list