Index finite lists of positive integers: Difference between revisions
Index finite lists of positive integers (view source)
Revision as of 17:00, 5 November 2020
, 3 years agooops
(add FreeBASIC) |
m (oops) |
||
Line 24:
Make the ''rank'' function as a [[wp:bijection| <u>bijection</u>]] and show ''unrank(n)'' for <big>'''n'''</big> varying from '''0''' to '''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}}==
|