BTW, here is a python solution for comparison:
from itertools import groupby
text = '''\
1 aaa 10 0.2
2 bbb 2 1
3 ccc 5 5
4 ddd 22 0.987
5 eee 1 0.987
6 fff 100 1
7 ggg 20 0.4'''
lines = [line.split() for line in text.split('\n')]
lines.sort(key=lambda d: float(d[3]), reverse=True)
for r, (k, g) in enumerate(groupby(lines, key=lambda d: d[3])):
for line in g:
print r+1, ' '.join(line)
On 5/8/06, June Kim <[EMAIL PROTECTED]> wrote:
Hello
Firstly, have a look at my quick and dirty code:
t=: 0 : 0
1 aaa 10 0.2
2 bbb 2 1
3 ccc 5 5
4 ddd 22 0.987
5 eee 1 0.987
6 fff 100 1
7 ggg 20 0.4
)
t2=:;:;._2
r=:] i.~ [: ~. \:~
keyf=:[: {:"1 >@(".each)@ {:"1
tt=:keyf (\:@[ { ":each@(<"0)@>:@[EMAIL PROTECTED] ,. ]) ]
sp=: [ , ' ' , ]
line=:>@(<@sp&>/)"1
[EMAIL PROTECTED]@t2 t
1 3 ccc 5 5
2 2 bbb 2 1
2 6 fff 100 1
3 4 ddd 22 0.987
3 5 eee 1 0.987
4 7 ggg 20 0.4
5 1 aaa 10 0.2
Suppose there is a given string table t, and the result should be a
table(shown as in the final result above), descendingly-sorted with
the last number in each line, with the ranks at the first column,
where the rank starts from 1 and when there is a tie the rank doesn't
increase and the next rank starts from current rank plus one.
I am not quite satisfied with my code. Particularly, I don't like that
there are several places doing sorting(\: and keyf, r), hence some
overhead of computation.
Any guidance to improve the code, whether to simplify or optimize, or
both, will be greatly appreciated.
June
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm