good chemistry is complicated,and a little bit messy -LW PerlMonks

Re: (Golf) Per Mutations

by Masem (Monsignor)
 on May 04, 2001 at 18:57 UTC ( #77970=note: print w/replies, xml ) Need Help??

in reply to (Golf) Per Mutations

A non-recursive solution, 138 characters, and doesn't suffer from problems with repeated elements.
```#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my @b = p( 1, 2, 3 );
print Dumper( @b );

sub p {
my@a=[];for(0..@_){@a=map{my@b=@\$_;my@c=@_;for(@b){my\$e=\$_;for(1..@c
+){my\$d=shift@c;last if\$d eq\$e;push @c,\$d}}@c?map{[@b,\$_]}@c:[@b]}@a}@
+a
}

Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re (tilly) 2: (Golf) Per Mutations
by tilly (Archbishop) on May 04, 2001 at 19:16 UTC
Non-recursive, handles repeated elements?
```sub permute{
@r=[];\$e=\$_,@r=map{@a=@\$_;map{@b=@a;splice(@b,\$_,0,\$e);[@b]}0..@a}@r f
+or@_;@r
}
77 chars...

UPDATE
75...

```sub permute{
@r=[];\$e=\$_,@r=map{@a=@\$_;map[@a[0..\$_-1],\$e,@a[\$_..\$#a]],0..@a}@r for
+@_;@r
}

Create A New User
Node Status?
node history
Node Type: note [id://77970]
help
Chatterbox?
 [trahulprajapati]: Hello All, Good Morning

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-06-28 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (637 votes). Check out past polls.