Very interesting! It took me a while to figure out what you were doing here - basically just assuming that the next in the sequence must be found at the start of the remaining options, choosing one that is either the most occurrences at starts of remaining lists or first seen, removing it from the options, then repeat? It seems like you can simplify that a bit though. (I'm not sure what the 1e9 was about unless that was from an earlier attempt?)
sub fancy_algorithm {
my @args= @_;
p @args;
my @order;
while (@args) {
my ($pick, %score, %seen_order);
my $see = 1;
$seen_order{$_} += $see++ for map @$_, @args;
for (@args) {
my $n= --$score{$_->[0]};
$pick= $_->[0] if !defined $pick or $n < $score{$pick}
or $n == $score{$pick} && $seen_order{$_->[0]} < $seen_ord
+er{$pick};
}
push @order, $pick;
$_ = [ grep $pick ne $_, @$_ ] for @args;
@args = grep @$_, @args;
}
return \@order;
}
It seems to work in every case I've tried, though it gives less ideal results with:
my @lists= (
[qw( S T )],
[qw( S U )],
[qw( Y Z )],
[qw( X Y )],
[qw( W X )],
[qw( V W )],
[qw( U V )],
);
# S U V W X Y T Z
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.