Index finite lists of positive integers: Difference between revisions

m
oops
(add FreeBASIC)
m (oops)
Line 24:
Make the &nbsp; ''rank'' &nbsp; function as a &nbsp; [[wp:bijection| <u>bijection</u>]] &nbsp; and show &nbsp; ''unrank(n)'' &nbsp; for &nbsp; <big>'''n'''</big> &nbsp; varying from &nbsp; '''0''' &nbsp; to &nbsp; '''10'''.
<br><br>
 
=={{header|D}}==
This solution isn't efficient.
{{trans|Python}}
<lang d>import std.stdio, std.algorithm, std.array, std.conv, std.bigint;
 
BigInt rank(T)(in T[] x) pure /*nothrow*/ @safe {
return BigInt("0x" ~ x.map!text.join('F'));
 
BigInt[] unrank(BigInt n) pure /*nothrow @safe*/ {
string s;
while (n) {
s = "0123456789ABCDEF"[n % 16] ~ s;
n /= 16;
}
return s.split('F').map!BigInt.array;
 
void main() {
immutable s = [1, 2, 3, 10, 100, 987654321];
s.writeln;
s.rank.writeln;
s.rank.unrank.writeln;
}</lang>
{{out}}
<pre>[1, 2, 3, 10, 100, 987654321]
37699814998383067155219233
[1, 2, 3, 10, 100, 987654321]
</pre>
 
=={{header|FreeBASIC}}==
Line 125 ⟶ 155:
2591460030 [19, 361]
9576882 [1, 2, 1, 2, 3, 1]</pre>
 
=={{header|D}}==
This solution isn't efficient.
{{trans|Python}}
<lang d>import std.stdio, std.algorithm, std.array, std.conv, std.bigint;
 
BigInt rank(T)(in T[] x) pure /*nothrow*/ @safe {
return BigInt("0x" ~ x.map!text.join('F'));
 
BigInt[] unrank(BigInt n) pure /*nothrow @safe*/ {
string s;
while (n) {
s = "0123456789ABCDEF"[n % 16] ~ s;
n /= 16;
}
return s.split('F').map!BigInt.array;
 
void main() {
immutable s = [1, 2, 3, 10, 100, 987654321];
s.writeln;
s.rank.writeln;
s.rank.unrank.writeln;
}</lang>
{{out}}
<pre>[1, 2, 3, 10, 100, 987654321]
37699814998383067155219233
[1, 2, 3, 10, 100, 987654321]
</pre>
 
=={{header|Go}}==
781

edits