in reply to Speeding permutation counting
.. i.e, not just look at all the pairs of characters in an individual string..string A: 0 1 0 1 string B: 1 1 0 1 | | | | | | | +--> 11 | | +----> 00 | +------> 11 +--------> 01
Splitting each string into an array seems wasteful, as arrays are much more memory-heavy than strings. You can use substr to index into any position in the string to get individual characters.
Also, instead of 4 different cases of logic involving 4 similarly-named variables $c00 through $c11, you can use a hash to really simplify the code:
Another cute solution is to use chop to trim off characters at a time from both strings. It generates these character-pairs in reverse order, but since you only care about the final count, it's ok. Since it modifies the string, you have to do it on a copy.my @data = qw[ 111011001010011110100010100001 111010000010010110000010000001 000101011100001000110101110000 000101111101001001111101111110 111011001010111110100010100001 000100010100000000010001010000 ]; use Data::Dumper; for my $i (0 .. $#data) { for my $j ($i+1 .. $#data) { my %counts; $counts{ substr($data[$i],$_,1) . substr($data[$j],$_,1) }++ for 0 .. length($data[$i]) - 1; print Dumper \%counts; } }
for my $i (0 .. $#data) { for my $j ($i+1 .. $#data) { my %counts; my ($str1, $str2) = @data[$i,$j]; $counts{ chop($str1) . chop($str2) }++ while length($str1) and length($str2); print Dumper \%counts; } }
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Speeding permutation counting
by albert (Monk) on Jul 18, 2007 at 14:57 UTC | |
by ikegami (Patriarch) on Jul 18, 2007 at 15:44 UTC | |
by albert (Monk) on Jul 18, 2007 at 16:23 UTC | |
by ikegami (Patriarch) on Jul 18, 2007 at 17:37 UTC |