Post Reply 
Ranks calculation
11-01-2024, 07:41 AM
Post: #8
RE: Ranks calculation
(10-31-2024 08:42 PM)Albert Chan Wrote:  Note that Python has 0-based array.
If code were for Python use, I would assign r back to the ranking list, instead of r+1

Lua array is 1-based, and probably easier to port to HP Prime

Code:
function ranking(m)
    local m2, m3, lo, hi = {}, {}, 1, #m
    for i=1, hi do m2[i] = {m[i],i} end
    table.sort(m2, function(x,y) return x[1]<y[1] end)
    for i=1, hi do
        local j = i+1
        if i<hi and m2[i][1]==m2[j][1] then continue end
        local r = (lo+i)/2
        for k=lo, i do m3[m2[k][2]] = r end
        lo = j
    end
    return m3
end

lua> pprint( ranking{1,3,5,1,4,5,6,1} )
{ 2, 4, 6.5, 2, 5, 6.5, 8, 2 }

Thank you very much, Albert Chan. I have translated your Python program into a Cas program:

Code:

#cas
ranking(m):=
BEGIN
LOCAL hi, m2, m3, lo, j, ii, r, k;
hi:= length(m);
m2:=zip(m, range(hi).+1);
m2:=sort(m2);
lo:=1;
m3:=m;
FOR ii FROM 1 TO hi DO
j:=ii+1;
IF j≠hi AND m2[ii][1]==m2[j][1] THEN
CONTINUE;
END;
r:=(lo+ii)/2;
FOR k FROM lo TO ii DO
m3[m2[k][2]]:=r;
END;
lo:=j;
END;
RETURN m3;
END;
#end
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Ranks calculation - robmio - 10-31-2024, 01:39 PM
RE: Ranks calculation - Albert Chan - 10-31-2024, 02:38 PM
RE: Ranks calculation - robmio - 10-31-2024, 02:46 PM
RE: Ranks calculation - robmio - 10-31-2024, 02:50 PM
RE: Ranks calculation - Albert Chan - 10-31-2024, 04:41 PM
RE: Ranks calculation - robmio - 10-31-2024, 08:16 PM
RE: Ranks calculation - Albert Chan - 10-31-2024, 08:42 PM
RE: Ranks calculation - robmio - 11-01-2024 07:41 AM
RE: Ranks calculation - robmio - 11-01-2024, 09:04 AM
RE: Ranks calculation - Albert Chan - 11-01-2024, 09:38 AM
RE: Ranks calculation - robmio - 11-01-2024, 10:03 AM
RE: Ranks calculation - Albert Chan - 11-01-2024, 10:34 AM
RE: Ranks calculation - robmio - 11-01-2024, 10:04 AM



User(s) browsing this thread: 3 Guest(s)