Jump to content

Index finite lists of positive integers: Difference between revisions

Added Perl example, 1st part
m (→‎{{header|Perl 6}}: list cannot start with zero in base-11 solution)
(Added Perl example, 1st part)
Line 460:
10 -> [1, 1] -> 10
</pre>
 
=={{header|Perl}}==
The base-11 approach requires <code>bigint</code> pragma for all but trivial lists. Using <code>ntheory</code> module for base conversions.
{{trans|Perl 6}}
<lang perl>use bigint;
use ntheory qw(fromdigits todigitstring);
use feature 'say';
 
sub rank { join '', fromdigits(join('a',@_), 11) }
sub unrank { split 'a', todigitstring(@_[0], 11) }
 
say join ' ', @n = qw<12 11 0 7 9 15 15 5 7 13 5 5>;
say $n = rank(@n);
say join ' ', unrank $n;</lang>
{{out}}
<pre>12 11 0 7 9 15 15 5 7 13 5 5
16588666500024842935939135419
12 11 0 7 9 15 15 5 7 13 5 5</pre>
 
=={{header|Perl 6}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.