Longest increasing subsequence: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: adding "Perl 6" in the subsection titles)
m (Swapped D versions)
Line 65: Line 65:
(0 2 6 9 11 15)</pre>
(0 2 6 9 11 15)</pre>
=={{header|D}}==
=={{header|D}}==
===Simple Version===
{{trans|Haskell}}
Uses the second powerSet function from the Power Set Task.
<lang d>import std.stdio, std.algorithm, power_set2;

T[] lis(T)(T[] items) /*pure nothrow*/ {
//return items.powerSet.filter!isSorted.max!q{ a.length };
return items
.powerSet
.filter!isSorted
.minPos!q{ a.length > b.length }
.front;
}

void main() {
[3, 2, 6, 4, 5, 1].lis.writeln;
[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15].lis.writeln;
}</lang>
{{out}}
<pre>[2, 4, 5]
[0, 2, 6, 9, 11, 15]</pre>

===Patience sorting===
===Patience sorting===
{{trans|Python}}
{{trans|Python}}
Line 106: Line 128:
d.writeln;
d.writeln;
}</lang>
}</lang>
The output is the same.
{{out}}
<pre>[2, 4, 5]
[0, 2, 6, 9, 11, 15]</pre>


===Faster Version===
===Faster Version===
Line 162: Line 182:
[0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15]])
[0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15]])
d.writeln;
d.writeln;
}</lang>
The output is the same.

===Simple Version===
{{trans|Haskell}}
Uses the second powerSet function from the Power Set Task.
<lang d>import std.stdio, std.algorithm, power_set2;

T[] lis(T)(T[] items) /*pure nothrow*/ {
//return items.powerSet.filter!isSorted.max!q{ a.length };
return items
.powerSet
.filter!isSorted
.minPos!q{ a.length > b.length }
.front;
}

void main() {
[3, 2, 6, 4, 5, 1].lis.writeln;
[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15].lis.writeln;
}</lang>
}</lang>
The output is the same.
The output is the same.