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:

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;

        - tye (but my friends call me "Tye")

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 ) );


In reply to (tye)Re: Generate all possibilities by tye
in thread Generate all possibilities by lostcause

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.