function for greatest object in list less than or equal to a value

BNig bernd.niggemann at uni-wh.de
Mon Oct 12 13:12:33 EDT 2015


OK, I will throw this in for a good measure


-----------------------------------------------------
function closestSmallerValue @pList, pMax
   -- this assumes pList to be sorted ascending AND consisting of numbers
   -- AND no empty items
   -- here only testing for SMALLER, not <=
   
   put the number of items of pList into tNoItems
   
   -- if pMax is > then value of last item of pList
   if pMax > tNoItems and pMax > item tNoItems of pList then return item
tNoItems of pList
   
   -- if pMax is smaller then value of first item
   if pMax <= item 1 of pList then return "error, no smaller value found"
   
   -- in case the number of items < pMax but pList contains valid range
   put min(pMax, tNoItems) into tStartItem
   
   -- try sort of a binary search
   return closeIn(pList,1,tStartItem,pMax)
end closestSmallerValue

function closeIn @pList pLowerBounds, pUpperBounds, pMax
   put pUpperBounds - pLowerBounds into tSpan
   put tSpan div 2 + pLowerBounds into tNewItem
   put item tNewItem of pList into tNewValue
   if tNewValue > pMax then
      put tNewItem into pUpperBounds
   else
      put tNewItem into pLowerBounds
   end if
   if pUpperBounds - pLowerBounds < 2 then
      if item pLowerBounds of pList < pMax then
         return item  pLowerBounds  of pList
      else
         return item  pLowerBounds -1  of pList
      end if
   else
      get closeIn (pList, pLowerBounds, pUpperBounds, pMax)
   end if
end closeIn
-----------------------------------------------------

BTW, what is a tick? Is that the imperial gallon of time? Or more a pint, a
fluid ounce?
I do kow that ticks bite and :)

No guarantees, did test it though with various lists up to 100000 items, but
what the tick... :)
My first attempt at something like "binary search" so please be forgiving.

Kind regards
Bernd



--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/function-for-greatest-object-in-list-less-than-or-equal-to-a-value-tp4697221p4697266.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list