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.br...@livecode.com>
LiveCode Open Source Team

LiveCode on reddit: https://reddit.com/r/livecode

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to