Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Derangements iterator (handles duplicate values)

by Roy Johnson (Monsignor)
on Jan 03, 2006 at 19:34 UTC ( [id://520703]=note: print w/replies, xml ) Need Help??


in reply to Derangements iterator

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-20 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found