Index finite lists of positive integers: Difference between revisions
Index finite lists of positive integers (view source)
Revision as of 20:30, 7 September 2018
, 5 years agoAdded Perl example, 1st part
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: list cannot start with zero in base-11 solution) |
SqrtNegInf (talk | contribs) (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}}==
|