I've come up with a solution that handles duplicate values properly: that is,
a b b a
generates exactly
b a a b
I wrote it as a recursive sub, and then converted it to be an iterator.
use strict;
use warnings;
# The recursive one
sub derange {
# First argument is optional arrayref of original values; if not pro
+vided,
# build it from arg list
my @orig = ref $_[0] ? @{shift(@_)} : @_;
if (@_ == 0) { return [] }
if (@_ == 1) {
return $_[0] eq $orig[0] ? () : [$_[0]];
}
# Generate a derangement by extracting each element then mapping it
+as the head
# to the derangement of the everything else
# Swaps that would cause a value to match its corresponding original
+ value are skipped
my %seen;
map {
if ($orig[$_] ne $_[0] and $orig[0] ne $_[$_] and not $seen{$_[$_]
+}++) {
my $swap_i = $_;
map [$_[$swap_i], @$_]
, derange([@orig[1..$#orig]], @_[0..$swap_i-1,$swap_i+1..$#_]);
}
else {
();
}
} (0..$#_);
}
# The iterator version
sub derange_iter {
# First argument is optional arrayref of original values; if not pro
+vided,
# build it from arg list
my @orig = ref $_[0] ? @{shift(@_)} : @_;
# Base cases get assigned to an array, which the iterator shifts thr
+ough
if (@_ == 0) {
my @base_case = ([]);
return sub { shift @base_case };
}
elsif (@_ == 1) {
my @base_case = $_[0] eq $orig[0] ? () : [$_[0]];
return sub { shift @base_case };
}
# otherwise..
my %seen;
my @list = @_;
my @sub_iter = map {
if ($orig[$_] ne $list[0] and $orig[0] ne $list[$_] and not $seen{
+$list[$_]}++) {
my $swap_i = $_;
sub {
my $cdr_iter = derange_iter([@orig[1..$#orig]], @list[0..$swap
+_i-1,$swap_i+1..$#list]);
sub {
my $cdr = $cdr_iter->();
if ($cdr) { return [$list[$swap_i], @$cdr] }
else { return () }
};
}
}
else {
();
}
} (0..$#_);
# Grab and unwrap an iterator from the list
my $iter = (shift @sub_iter)->();
return sub {
my $rval;
$iter = (shift @sub_iter)->()
until ($rval = $iter->() or @sub_iter == 0);
return $rval;
}
}
@ARGV or @ARGV = 1..5;
my $i = derange_iter(@ARGV);
for (1..50) {
my $val = $i->() or last;
print "@$val\n";
}
#print "\nShould be:\n";
#print "@$_\n" for sort {"@$a" cmp "@$b"} derange(@ARGV);
The commented-out code at the end is for verifying that the iterator generates the same output as the recursive version. But don't uncomment it if you want to run on large inputs. The iterator will spit out the first 50 values almost immediately; the recursive version will hang/crash.
Caution: Contents may have been coded under pressure.
-
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.
|