in reply to (jeffa) Re: Generate all possibilities
in thread Generate all possibilities
None of those do what was asked for. larryk's fine suggestion of (Golf) Ordered Combinations does what was asked for but requires that all 1,048,576 ten-character strings be stored in memory for you to get the answer. With Perl arrays, this takes several hundred MB (in my quick test) and so could be a real problem. If you just write the combinations to a file, then you end up with about a 10MB file, which isn't a big deal.
So here is a quick hack to do that:
- tye (but my friends call me "Tye")sub count { my( $length, $alphabet )= @_; my @alphabet= split //, $alphabet; my @digit= (0) x $length; my $seq= $alphabet[0] x $length; while( 1 ) { print $seq,$/; my $pos= 0; while( @alphabet <= ++$digit[$pos] ) { substr( $seq, $pos, 1 )= $alphabet[ $digit[$pos]= 0 ]; return if $length <= ++$pos; } substr( $seq, $pos, 1 )= $alphabet[ $digit[$pos] ]; } } count( @ARGV ); # count( $_, "ACGT" ) for 8..10;
Update: And here is a context-free iterator solution:
sub count { my( $alphabet, $seq )= @_; my $base= length($alphabet); my $pos= 0; my $val; while( $base <= ( $val= 1 + index( $alphabet, substr($seq,$pos,1) + ) ) ) { substr( $seq, $pos, 1 )= substr( $alphabet, 0, 1 ); return if length($seq) <= ++$pos; } substr( $seq, $pos, 1 )= substr( $alphabet, $val, 1 ); return $seq; } my( $len, $alphabet )= @ARGV; my $seq= substr($alphabet,0,1) x $len; do { print $seq, $/; } while( $seq= count( $alphabet, $seq ) );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: Generate all possibilities
by lostcause (Monk) on Jun 26, 2001 at 02:12 UTC |