If you just match up the givers one by one against randomly chosen recipients, you should be OK most of the time. A test is added to force another choice when the giver :
  1. and randomly chosen recipient are the same person
  2. The giver has given to the recipient before

Unfortunately, the likly hood of completing the task diminishes as the number of people in the list compared to the number of people that they have given to becomes close. The iteration count is added in order to stop an infinite loop if a solution cannot be found. I have decided to terminate the script if a solution is impossible but another loop could be added to keep going until a solution was found.

#! /usr/bin/perl -w 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 @allPeople = sort keys %people; my %recipients; @recipients{@allPeople}=undef; srand; # randomize! foreach (@allPeople) { my @remainingPeople = keys %recipients; my $randPerson; my $iterations; do { $randPerson = $remainingPeople[int (rand ($#remainingPeople))] +; $iterations++; die ("ERROR: Failed to generate list\n") unless ($iterations < +$#allPeople); } until (testRecipient($_, $randPerson)); print "$_ can give to $randPerson\n"; delete $recipients {$randPerson}; } sub testRecipient { my ($santa, $recipient) = @_; my $arrayref = $people{$santa}; # test for self giving return 0 if ($santa eq $recipient); #test for giving to the given foreach (@$arrayref){ return 0 if ($_ eq $recipient); } return 1; }

Comments on the code (especially dereferncing the anonymous array reference contained in the hash element) is most welcome!


In reply to Re: Kris Kringle / Secret Santa by inman
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.