Note that a solution doesn't always exist. With 6 people, it's easy to see it's possible that after 3 years, it could happen there's no solution.

Here's a solution based on a regex. It may run a long time if you have 50 couples celebrating their 50th Christmas though.

#!/usr/bin/perl use strict; use warnings; my %people = ( jim => ['betty', 'claire'], betty => ['frank', 'jim' ], frank => ['claire', 'john' ], claire => ['john', 'betty' ], john => ['jim', 'frank' ], nancy => ['', '' ], # new girl ); my @names = keys %people; my $str = ""; my $re = ""; my $i; foreach my $name (@names) { $str .= "$name:"; my %not; @not {$name, @{$people {$name}}} = (); $str .= join "," => grep {!exists $not {$_}} @names; $str .= "\n"; $re .= ".*\\b"; $re .= "(?!\\$_)" for 1 .. $i ++; $re .= "(\\w+)\\b.*\n"; } if (my @a = $str =~ $re) { foreach my $name (@names) { printf "$name picks %s\n" => shift @a; } } else { print "No solution possible.\n"; } __END__ nancy picks frank john picks betty claire picks jim betty picks claire jim picks john frank picks nancy

Abigail


In reply to Re: Kris Kringle / Secret Santa by Abigail-II
in thread Kris Kringle / Secret Santa by BigLug

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.