Keep It Simple, Stupid PerlMonks

Re: Re: Character Combinations

by Limbic~Region (Chancellor)
 on Aug 22, 2003 at 23:06 UTC ( #285953=note: print w/replies, xml ) Need Help??

in reply to Re: Character Combinations

chunlou,
Permutations are not combinations. For instance - say I wanted to find all combinations of 4 character strings with each position able to be 26 different letters. This is what I believe is being asked for. I am pretty sure there is an iterative solution - but all I can come up with right now are recursive ones.

Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Re: Character Combinations
by chunlou (Curate) on Aug 22, 2003 at 23:36 UTC
You mean this?
```use Algorithm::FastPermute;
my @c = ('a'..'c');
for my \$i (0..2) {
for my \$j (0..2) {
my @c = @c[\$i..\$j] if \$i <= \$j;
permute {print @c , "\n"} @c;
}
}

__END__
a
ab
ba
abc
acb
cab
bac
bca
cba
b
bc
cb
c
chunlou,
Considering that missed ca - no. It also doesn't work if you change
my @c = ('a'..'c');
to
my @c = ('a'..'z');

Permutations are not combinations.

Silly me. Should have consulted with this: Permutations and combinations.
```use Algorithm::FastPermute;

sub combinations {
return [] unless @_;
my \$first = shift;
my @rest = combinations(@_);
return @rest, map { [\$first, @\$_] } @rest;
}

for(combinations('a'..'c')){
my @c = @{\$_};
permute {print @c , "\n"} @c;
}

__END__
c
b
bc
cb
a
ac
ca
ab
ba
abc
acb
cab
bac
bca
cba

Create A New User
Node Status?
node history
Node Type: note [id://285953]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-03-17 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (223 votes). Check out past polls.

Notices?