[perl-python] generate all possible pairings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    [perl-python] generate all possible pairings

    20050226 exercise: generate all possible pairings

    given a list that is a set partitioned into subsets, generate a list
    of all possible pairings of elements in any two subset.

    Example:

    genpair( [[9,1],[5],[2,8,7]] );

    returns:

    [[5,8],[9,5],[1,5],[9,2],[9,7],[1,8],[1,7],[5,2],[1,2],[9,8],[5,7]]

    (we do not assume the given set has order, so the result is not
    ordered.)

    Perl code and Python code will be posted in 2 days.

    This is brought to you by the perl-python community. To subscribe, see


    Xah
    xah@xahlee.org


  • Mike Meyer

    #2
    Re: [perl-python] generate all possible pairings

    "Xah Lee" <xah@xahlee.org > writes:
    [color=blue]
    > This is brought to you by the perl-python community. To subscribe, see
    > http://xahlee.org/perl-python/python.html[/color]

    assert len(perl-python community) == 1

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Chris Mattern

      #3
      Re: [perl-python] generate all possible pairings

      Mike Meyer wrote:
      [color=blue]
      > "Xah Lee" <xah@xahlee.org > writes:
      >[color=green]
      >> This is brought to you by the perl-python community. To subscribe, see
      >> http://xahlee.org/perl-python/python.html[/color]
      >
      > assert len(perl-python community) == 1
      >
      > <mike[/color]
      Apparently there are actually people who subscribe. Maybe they're
      people in search of a good laugh...

      I suppose they could be sock-puppets, but even for Xah, inventing
      sock-puppets just to up the subscription count of your mail list
      seems a bit much...

      --
      Christopher Mattern

      "Which one you figure tracked us?"
      "The ugly one, sir."
      "...Could you be more specific?"

      Comment

      • Xah Lee

        #4
        Re: [perl-python] generate all possible pairings

        Answer to the previous exercise.


        # perl
        sub genpair ($) {
        my $partiSet = $_[0];
        my @result;
        for (my $head =0; $head <= ((scalar @$partiSet)-2); $head++ ) {
        for (my $tail = $head+1; $tail <= ((scalar @$partiSet)-1); $tail++
        ) {
        foreach my $ii (@{$partiSet->[$head]}) {
        foreach my $jj (@{$partiSet->[$tail]}) {
        push @result, [$ii,$jj]
        }
        }
        }
        }
        return \@result;
        }

        @# python
        @def genpair (partiSet):
        @ result=[]
        @ for head in range(len(parti Set)-1):
        @ for tail in range(head+1,le n(partiSet)):
        @ for ii in partiSet[head]:
        @ for jj in partiSet[tail]:
        @ result.append(( ii,jj))
        @ return result
        @
        @# by Sean Gugler
        @def genpair2 (partiSet):
        @ return [(a,b) for s in range(len(parti Set))
        @ for t in range(s)
        @ for a in partiSet[t]
        @ for b in partiSet[s]]
        @
        @ll=( [[9,1],[5],[2,8,7]])
        @t=genpair(ll)
        @t2=genpair2(ll )
        @if (t.sort()==t2.s ort()): print 'yay'


        Xah
        xah@xahlee.org


        Comment

        Working...