all_sequences.pl (Was: Perl - permute?)

Discussion in 'Perl Misc' started by kj, Feb 9, 2005.

  1. kj

    kj Guest

    In <1107709033.0d15271ffc45f8463e1fcde7dddd2c79@bubbanews> Ej <> writes:

    >Hi,
    >How can I take an array (a,b,c,d,e) and list ALL possible [3 LETTER] combos
    >of these letters, I want the output to look like


    >abc
    >acb
    >aab
    >aaa


    Iterative or recursive, take your pick.

    sub all_sequences {
    my ($alphabet, $n) = @_;
    return [] unless @$alphabet;
    return [''] unless $n;

    my @words = @$alphabet;

    for (2..$n) {
    for my $i (reverse 0..$#words) {
    my $word = $words[$i];
    splice @words, $i, 1, map "${word}$_", @$alphabet;
    }
    }

    return \@words;
    }

    sub all_sequences_recursive {
    my ($alphabet, $n) = @_;
    return [] unless @$alphabet;
    return [''] unless $n;
    return
    [ map { my $word = $_; map "${word}$_", @$alphabet }
    @{all_sequences_recursive($alphabet, $n-1)} ]
    }

    __END__

    DB<1> x all_sequences_recursive([ qw(a b c d e) ], 3);
    0 ARRAY(0x23dae4)
    0 'aaa'
    1 'aab'
    2 'aac'

    ....

    122 'eec'
    123 'eed'
    124 'eee'
    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Feb 9, 2005
    #1
    1. Advertising

  2. kj

    Guest

    kj wrote:
    > In <1107709033.0d15271ffc45f8463e1fcde7dddd2c79@bubbanews> Ej

    <> writes:
    >
    > >Hi,
    > >How can I take an array (a,b,c,d,e) and list ALL possible [3 LETTER]

    combos
    > >of these letters, I want the output to look like

    >
    > >abc
    > >acb
    > >aab
    > >aaa

    >
    > Iterative or recursive, take your pick.
    >
    > sub all_sequences {
    > my ($alphabet, $n) = @_;
    > return [] unless @$alphabet;
    > return [''] unless $n;
    >
    > my @words = @$alphabet;
    >
    > for (2..$n) {
    > for my $i (reverse 0..$#words) {
    > my $word = $words[$i];
    > splice @words, $i, 1, map "${word}$_", @$alphabet;
    > }
    > }
    >
    > return \@words;
    > }
    >
    > sub all_sequences_recursive {
    > my ($alphabet, $n) = @_;
    > return [] unless @$alphabet;
    > return [''] unless $n;
    > return
    > [ map { my $word = $_; map "${word}$_", @$alphabet }
    > @{all_sequences_recursive($alphabet, $n-1)} ]
    > }
    >
    > __END__
    >
    > DB<1> x all_sequences_recursive([ qw(a b c d e) ], 3);
    > 0 ARRAY(0x23dae4)
    > 0 'aaa'
    > 1 'aab'
    > 2 'aac'
    >
    > ...
    >
    > 122 'eec'
    > 123 'eed'
    > 124 'eee'
    > --
    > NOTE: In my address everything before the first period is backwards;
    > and the last period, and everything after it, should be discarded.


    Here is a solution that will make you sick.
    I cannot even say that it works because
    my computer got stuck on it:

    #!/usr/bin/perl

    use warnings;
    use strict;

    my @a = ('a' .. 'e');
    my $max = 5*5*5;
    my %results;
    while (scalar keys %results < $max)
    {
    my $r1 = $a[int rand(4)];
    my $r2 = $a[int rand(4)];
    my $r3 = $a[int rand(4)];
    my $result = "$r1$r2$r3";
    $results{$result}++ if not $results{$result} and print $result,
    "\n";
    }
    print "Here they are again, in order:\n";
    print $_, "\n" for sort keys %results;

    I think it should work eventually :)

    wana
    , Feb 9, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yuan Zhong

    function to permute a string

    Yuan Zhong, Aug 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    450
    CBFalconer
    Aug 5, 2004
  2. Michael McGarry

    Randomly permute a list of integers

    Michael McGarry, Aug 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    581
  3. Phlip
    Replies:
    17
    Views:
    231
    Rick DeNatale
    May 18, 2009
  4. PerlFAQ Server

    FAQ 4.51 How do I permute N elements of a list?

    PerlFAQ Server, Feb 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    156
    PerlFAQ Server
    Feb 7, 2011
  5. PerlFAQ Server

    FAQ 4.51 How do I permute N elements of a list?

    PerlFAQ Server, Mar 6, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    123
    PerlFAQ Server
    Mar 6, 2011
Loading...

Share This Page