NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

## All the Perl that's Practical to Extract and Report

### Journal of brian_d_foy (44)

Thursday August 26, 2004
12:32 PM

### Good morning kata

[ #20582 ]

Ziggy pointed me towards a kata for today, by Norman Walsh. He wants to count the permutations of the middle letters in "morning".

I decided to use a module by Tom Phoenix:

use List::Permutor;

my \$p = List::Permutor->new( split //, 'ornin' );

while( my @s = \$p->next )
{
\$hash{ join '', 'm', @s, 'g' }++;
}

\$, = "\n";
print keys %hash;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.

Full
Abbreviated
Hidden
• #### Permuting by hand(Score:2)

I decided to take a more manual approach, just to make sure the recursive lobes of my brain still work:

sub cycle {
my @chars = @_;
my @result;

## Simple case -- only one choice
return \$chars[0] if (@chars == 1);

for (1..@chars) {
my \$char = shift(@chars);
push @result, "\$char\$_" for (cycle(@chars));
push(@chars, \$char);
}

return @resu

• #### get count analytically(Score:2)

If, as stated, you just want the count and don't need to see the actual permutations spelled out, it is just a matter of simple arithmetic. The number of permutations of n unique objects is n!. If there are duplicate objects amongst the n, that number must be divided by k(i)! where k(i) is the number of identical objects for the i'th unique item. (That sounds complicated, but for "ornin" the number is 5!/(1!1!2!1!) since there is 1 o, 1 r, 2 n's, and 1 i. So, 120/(1x1x2x1) = 60. If the original word had