@@ -0,0 +1,1531 @@
+#! /usr/bin/python%0A%0A# vim: set ai sw=4:%0A%0Aimport random%0Aimport time%0A%0Afamily_list = %5B%5B%22Alia%22, %22Tanya%22%5D,%0A%09       %5B%22Nick%22, %22Ariana%22, %22Max%22%5D,%0A%09       %5B%22Paige%22, %22Ian%22, %22Kendra%22%5D%0A%09      %5D%0A%0A# Given a family_list, create 2 new data structures:%0A#   1) Just a raw list of all the names%0A#   2) A dictionary mapping from a name to a family identifier%0Afamily_dict = %7B%7D%0Anamelist = %5B%5D%0A%0Afamily_id = 0%0Afor family in family_list:%0A    for name in family:%0A%09family_dict%5Bname%5D = family_id%0A%09namelist.append(name)%0A    family_id += 1%0A%0A# Create a random pairing (really just a permutation of the input list).%0A# This is returned as a list of pairs of names.%0Adef pairing(input):%0A    leftlist = list(input)%0A    rightlist = list(input)%0A%0A    random.shuffle(rightlist)%0A    return zip(leftlist, rightlist)%0A%0A# Print a pairing nicely.%0Adef show_pairing(pairing):%0A    for p in pairing:%0A%09print %22%258s gives to %25s.%22 %25 p%0A%0A# Apply the constraints to a pairing.  If it meets the constraints,%0A# return True; otherwise, return False.%0Adef pairing_good(pairing):%0A    for p in pairing:%0A%09if p%5B0%5D == p%5B1%5D: return False%0A%09if family_dict%5Bp%5B0%5D%5D == family_dict%5Bp%5B1%5D%5D: return False%0A    return(True)%0A%0A# Keep creating pairings until we find a good one.%0Adef find_good_pairing(return_num_tries=False):%0A    num_tries = 0%0A    while (True):%0A%09p = pairing(namelist)%0A%09num_tries +=1%0A%09# print %22Trying pairing%22, show_pairing(p)%0A%09if pairing_good(p):%0A%09    print (%22Took %25d tries to get a good pairing.%22 %25 num_tries)%0A%09    if return_num_tries:%0A%09%09return (p, num_tries)%0A%09    else:%0A%09%09return p%0A%09# time.sleep(1)%0A

Add python program (really a collection of functions) which will choose a set of name pairings for a christmas gift exchange.