Index finite lists of positive integers: Difference between revisions
Index finite lists of positive integers (view source)
Revision as of 05:26, 10 May 2014
, 10 years ago→{{header|Perl 6}}: dealing with lists of length one + fusing the plain solution with the extra-credit one
(→{{header|Python}}: bijection) |
(→{{header|Perl 6}}: dealing with lists of length one + fusing the plain solution with the extra-credit one) |
||
Line 173:
=={{header|Perl 6}}==
<lang perl6>sub expand(Int $n is copy, Int $dimension where * >
return $n if $dimension == 1;
my @reversed-digits = gather while $n > 0 {
take $n % $dimension;
Line 183 ⟶ 184:
}, ^$dimension;
}
sub compress(*@n is copy where @n >
my $dimension = @n.elems;
return @n[0] if $dimension == 1;
reduce * * $dimension + *, 0, 0,
reverse gather while @n.any > 0 {
Line 194 ⟶ 196:
}
}
sub rank(@n) { compress compress(@n), +@n - 1}
sub unrank(Int $n) {
say my @list = (^10).roll((2..20).pick);
say my $rank = rank @list;
say unrank $rank;
{{out}}▼
for ^10 {
my @unrank = unrank $_;
say $_, " <-> [", @unrank, "] <-> ", rank @unrank;
}</lang>
<pre>0 0 <-> 0▼
1 0 <-> 1▼
0 0 0 <-> 2▼
1 0 0 <-> 3▼
0 1 <-> 4▼
1 1 <-> 5▼
2 0 0 <-> 6▼
0 1 0 <-> 7▼
0 0 0 0 <-> 8▼
1 0 0 0 <-> 9▼
0 0 0 0 0 <-> 10</pre>▼
▲{{out}}
<pre>1 2 2 6 5 7
77692871663419443
=={{header|Python}}==
|