Longest increasing subsequence: Difference between revisions
Content added Content deleted
(→{{header|Perl 6}}: adding perl translation) |
|||
Line 129: | Line 129: | ||
<pre>an L.I.S. of [3, 2, 6, 4, 5, 1] is [2, 4, 5] |
<pre>an L.I.S. of [3, 2, 6, 4, 5, 1] is [2, 4, 5] |
||
an L.I.S. of [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] is [0, 2, 6, 9, 11, 15]</pre> |
an L.I.S. of [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] is [0, 2, 6, 9, 11, 15]</pre> |
||
=={{header|Perl}}== |
|||
{{trans|Perl 6}} |
|||
<lang Perl>sub lis { |
|||
my @l = map [], 1 .. @_; |
|||
push @{$l[0]}, +$_[0]; |
|||
for my $i (1 .. @_-1) { |
|||
for my $j (0 .. $i - 1) { |
|||
if ($_[$j] < $_[$i] and @{$l[$i]} < @{$l[$j]} + 1) { |
|||
$l[$i] = [ @{$l[$j]} ]; |
|||
} |
|||
} |
|||
push @{$l[$i]}, $_[$i]; |
|||
} |
|||
my ($max, $l) = 0, []; |
|||
for (@l) { |
|||
($max, $l) = (scalar(@$_), $_) if @$_ > $max; |
|||
} |
|||
return @$l; |
|||
} |
|||
print join ', ', lis(qw(3 2 6 4 5 1));</lang> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |