Page 1 of 1
Get index of highest value
Posted: Thu Apr 26, 2007 10:45 pm
by xApple
This function (that I wrote) seams to be essential in several of my projects, all though I can't keep thinking there must be a more efficient way of doing it... anyone an idea ?
Code: Select all
function getIndexOfHighestValue numberArray
-- Example: Input="3,7,5,1"; Output="2"
put the itemDel into myOldDel
set the itemDel to comma
repeat with x = 1 to item 2 of line 1 of extents(numberArray)
put numberArray[x] & comma & x into line x of numberList
end repeat
sort lines of numberList descending numeric by item 1 of each
put item 2 of line 1 of numberList into theAnswer
set the itemDel to myOldDel
return theAnswer
end getIndexOfHighestValue
Re: Get index of highest value
Posted: Fri Apr 27, 2007 1:10 am
by Mark Smith
xApple wrote:This function (that I wrote) seams to be essential in several of my projects, all though I can't keep thinking there must be a more efficient way of doing it... anyone an idea ?
I haven't benchmarked them, but this is what I've come up with:
Code: Select all
function getIndexOfHighestValue numberArray
put max(numberArray) into tMax
repeat for each line L in the keys of numberArray
if numberArray[L] = tMax then return L
end repeat
end getIndexOfHighestValue
best,
Mark
Re: Get index of highest value
Posted: Fri Apr 27, 2007 1:14 am
by marielle
Code: Select all
function getIndexOfHighestValue numberItems
put max(numberItems) into tMax
put itemOffset("," & tMax & ",", "," & numberItems & ",") into tOff
return tOff
end getIndexOfHighestValue
If the input must be an array rather than a sequence of items
Code: Select all
function getIndexOfHighestValue arrayItems
put item 2 of line 1 of extents(arrayItems) into tMax
combine arrayItems with ","
put itemOffset("," & tMax & ",", "," & arrayItems & ",") into tOff
return tOff
end getIndexOfHighestValue
It's not really proposed as an improvement as Mark's solution is very efficient. You will need to benchmark both to get an idea of which one to use in which context.
If you need to know all indexes, not just the first one, then best is to adapt Mark's solution rather than this one. In Mark's code, "
if numberArray[L] = tMax then return L " would become
"if numberArray[L] = tMax then put L & cr after tList ... end repeat ... return tList"
Posted: Fri Apr 27, 2007 1:30 am
by marielle
funny, I found a way to rapidly remove duplicates in a list when playing with this:
Code: Select all
function list.deleteDuplicates pList
split pList by cr and space
combine pList by cr and space
return pList
end list.deleteDuplicates
pList:
3
7
5
1
7
5
6
returns :
1
3
5
6
7
Re: Get index of highest value
Posted: Fri Apr 27, 2007 1:45 am
by marielle
xApple wrote:Code: Select all
put the itemDel into myOldDel
...
set the itemDel to myOldDel
Note that this is not required. the itemdelimeter is automatically reset to the default (",") when the handler finishes executing. Cf the doc "Since the itemDelimiter is a local property, its value is reset to comma when the current handler finishes executing. It retains its value only for the current handler, and setting it in one handler does not affect its value in other handlers it calls."
Re: Get index of highest value
Posted: Fri Apr 27, 2007 6:06 pm
by xApple
marielle wrote:Note that this is not required. the itemdelimeter is automatically reset to the default (",") when the handler finishes executing. Cf the doc "Since the itemDelimiter is a local property, its value is reset to comma when the current handler finishes executing. It retains its value only for the current handler, and setting it in one handler does not affect its value in other handlers it calls."
Hey thanks, I dind't know that ^^
And yeah thanks both for the faster algorithm, I knew there was a simpler way ! Now I feel stupid I didn't come up with it in the first place : )
Re: Get index of highest value
Posted: Fri Apr 27, 2007 6:11 pm
by marielle
xApple wrote:Now I feel stupid I didn't come up with it in the first place : )
Don't feel bad about it. We didn't either come up with these in the first place... we both re-edited the post we initially wrote using the edit button

I used Mark's code to improve mine and he did the same. Consider it collaborative solution finding

.