in reply to counting instances of one array in another array
Build a hash that relates the permutation to its position, and use that to decide which count to increment:
#! perl -slw use strict; use Data::Dump qw[ pp ]; use Algorithm::Combinatorics qw[ variations_with_repetition ]; our $N //= 4; my @perms = map join( '', @$_ ), variations_with_repetition( [ 1 .. $N + ], $N ); my %lookup; $lookup{ $perms[ $_ ] } = $_ for 0 .. $#perms; my @counts = (0) x @perms; for( 1 .. 1e3 ) { my $observed = join '', map 1+int( rand $N ), 1 .. $N; ## random o +bservation ++$counts[ $lookup{ $observed } ]; } printf "%${N}s : %${N}d\n", $perms[ $_ ], $counts[ $_ ] for 0 .. $#perms;
A run:
C:\test>junk -N=3 111 : 41 112 : 37 113 : 35 121 : 27 122 : 32 123 : 31 131 : 43 132 : 34 133 : 45 211 : 43 212 : 35 213 : 40 221 : 47 222 : 38 223 : 32 231 : 46 232 : 44 233 : 30 311 : 33 312 : 37 313 : 30 321 : 30 322 : 49 323 : 35 331 : 42 332 : 32 333 : 32
|
|---|