Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

anyone know a good way to test whether a list which will always be a made up from the elements of two input lists satisfies the condition of being made up from interleaved elements from the two lists i.e. listAelement, listBelement, listAelement, listBelement i actually am taking the two input lists concatanating them and generating all permutations of the new list (can already do that bit) thanks, g i then want to go through this list of permutations and throw away all which aren't interleaved permutations as defined above leaving me with all possible interleaved permutations of the two lists

Replies are listed 'Best First'.
Re: interleaved lists check
by GrandFather (Saint) on Jun 16, 2006 at 10:17 UTC

    This sounds either like an XY Problem or like homework. Perhaps you should paint the broarder picture for us so we understand better what you are after?

    It seems it doesn't matter what order the list elements occur in so long as they alternate from list A and list B. Can the lists have duplicate entries (either between lists, or within a list)? Is it guaranteed that the lists differ by no more than 1 in the number of their elements?


    DWIM is Perl's answer to Gödel
      not homework but almost certainly an xy problem

      what i need is ALL the lists which you can make by interleaving elements from listA and listB

      why is another question entirley, i don't really know except that its for some form of memory experiment, the guy i work for asked me for a script to do all the permutations of a list which i gave him and then he asked me for this

      and yes your right the brute force method of just generating all the permutations and then selecting the ones i'm interested was assumption on my part of the easiest way to do it

        This still sounds like an answer to the wrong problem. For even modest sized lists the number of permutations is huge. However the following code using Math::Combinatorics should get you started:

        use strict; use warnings; use Math::Combinatorics; my @listA = qw(first second third); my @listB = qw(1 2 3); my $permA = Math::Combinatorics->new (data => [@listA]); # Note copy while (1) { my @listAperm = $permA->next_permutation (); last if ! @listAperm; my $permB = Math::Combinatorics->new (data => [@listB]); # Note co +py while (1) { my @listBperm = $permB->next_permutation (); last if ! @listBperm; my @interList1; my @interList2; my $aIndex = 0; my $bIndex = 0; while ($aIndex < @listAperm || $bIndex < @listBperm) { push @interList1, $listAperm[$aIndex] if $aIndex < @listAp +erm; push @interList2, $listBperm[$bIndex] if $bIndex < @listBp +erm; push @interList2, $listAperm[$aIndex++] if $aIndex < @list +Aperm; push @interList1, $listBperm[$bIndex++] if $bIndex < @list +Bperm; } print "@interList1\n"; print "@interList2\n"; } }

        DWIM is Perl's answer to Gödel
        Sounds like the guy you're working for may have an XY problem, too... (Does that mean you have an X^2Y^2 problem? Or would it be an X^2 + 2XY + Y^2 problem?)

        If you already have the means to generate all the permutations of a list, then I suspect that generating all permutations of each source list and then interleaving each pair of those would be much faster, much simpler, and much less memory-intensive than generating all permutations of the combined source lists and filtering out those which aren't interleaved.

Re: interleaved lists check
by Moron (Curate) on Jun 16, 2006 at 09:53 UTC
    # comparing @merged with @a and @b: my $pass = 1; for (my ($ai,$bi,$mi) = (0,0,0); $mi < $#merged; ) { ($a[$ai++] eq $merged[$mi++]) or $pass = 0 or last; ($b[$bi++] eq $merged[$mi++]) or $pass = 0 or last; }

    -M

    Free your mind

Re: interleaved lists check
by Anonymous Monk on Jun 16, 2006 at 09:43 UTC
    Why don't you generate the correct list and be done with it? This smells like a stupid homework assignment :(

    How (Not) To Ask A Question

Re: interleaved lists check
by davorg (Chancellor) on Jun 16, 2006 at 09:54 UTC

    (Removed as this smells too much like homework)

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg