Index finite lists of positive integers: Difference between revisions

Content added Content deleted
(Added Arturo implementation)
Line 48: Line 48:
[1, 2, 3, 10, 100, 987654321]
[1, 2, 3, 10, 100, 987654321]
</pre>
</pre>

=={{header|Arturo}}==

<lang rebol>rank: function [arr][
if empty? arr -> return 0
from.binary "1" ++ join.with:"0" map arr 'a -> repeat "1" a
]

unrank: function [rnk][
if rnk=1 -> return [0]
bn: as.binary rnk
map split.by:"0" slice bn 1 dec size bn => size
]

l: [1, 2, 3, 5, 8]

print ["The initial list:" l]

r: rank l
print ["Ranked:" r]

u: unrank r
print ["Unranked:" u]</lang>

{{out}}

<pre>The initial list: [1 2 3 5 8]
Ranked: 14401279
Unranked: [1 2 3 5 8]</pre>


=={{header|D}}==
=={{header|D}}==