Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is just a simple implementation of Patience Sorting, finding the LIS using backrefs, and doing the final merge phase to produce a sorted list. I didnt bother with anything special like binsearch or what not.

use strict; use warnings; use List::Util qw(shuffle); use Text::Wrap qw(wrap); sub patience { my ($deck)=@_; my @piles; CARD: foreach my $card (@$deck) { foreach my $pid ( 0..$#piles ) { if ( $card < $piles[$pid][-1][0] ) { push @{$piles[$pid]}, [ $card, $pid ? $piles[$pid-1][-1] : undef ]; next CARD; } } push @piles,[ [ $card, @piles ? $piles[-1][-1] : undef ] ]; } return \@piles } sub lis { my ($piles)= @_; my $iter= $piles->[-1][-1]; my @lis; while ($iter) { push @lis, $iter->[0]; $iter= $iter->[1]; } @lis=reverse @lis; return \@lis } sub inorder { my ($piles)= @_; my @inorder=(pop(@{$piles->[0]})->[0]); while (@$piles) { my $min=0; foreach my $pid (0..$#$piles) { if (@{$piles->[$pid]} && $piles->[$pid][-1][0] < $piles->[$min][-1][0] ){ $min=$pid; } } push @inorder,(pop(@{$piles->[$min]})->[0]); shift @$piles while @$piles && !@{$piles->[0]}; pop @$piles while @$piles && !@{$piles->[-1]}; } return \@inorder } sub print_ary { my ($head,$list)=@_; print wrap($head,"",join ", ",@$list),"\n---\n"; } my @deck= shuffle(1..52); my $piles=patience(\@deck); my $lis=lis($piles); my $inorder=inorder($piles); print_ary("DECK : ",\@deck); print_ary("LIS : ",$lis); print_ary("INORDER: ",$inorder);

Ive been trying to put together a vEB tree implementation to play with, but so far the documentation on it that I've found hasnt been as great as it looks at first glance. Notably ive been going a little barmy working out how you update the "max" property correctly when deleting the maximum element in a tree. Probably i just tried to work on it too long and if i rereview the documentation again it will clarify itself...

---
$world=~s/war/peace/g


In reply to Re: Patience Sorting To Find Longest Increasing Subsequence by demerphq
in thread Patience Sorting To Find Longest Increasing Subsequence by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found