Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- # "secret santa" gift pairs: sets of spouses, who buys for who?
- # can't have a spouse buying for spouse, and can't have person
- # buy for self.
- # brute force algorithm below is probably easiest. With only
- # a few values, performance doesn't matter, so this worked ok,
- # but I think that probably setting them up as disjoint sets or
- # just a dictionary/mapping with a key for each user and
- # possible matchups as a list as the value and then just
- # randomizing would be faster.
- import pprint, random
- pairs = (('joe', 'sandy'),
- ('michael', 'cheryl'),
- ('jamieson', 'jennifer'),
- ('matthew', 'dannii'))
- # convert to straight list
- all = " ".join([" ".join(pair) for pair in pairs]).split()
- def randomize_sample(sample):
- while True:
- newsample = zip(random.sample(sample, len(sample)),random.sample(sample,len(sample)))
- if [(a,b) for a,b in newsample if a==b]:
- # skip this matchup, someone is buying for themselves
- continue
- if [pair for pair in newsample if pair in pairs]:
- # skip this matchup, someone has their spouse
- continue
- if [(a,b) for (a,b) in newsample if (b,a) in pairs]:
- # skip this matchup, someone has their spouse (reversed pair)
- continue
- # we made it this far, this sample is ok!
- break
- return newsample
- for i in xrange(10):
- sample = randomize_sample(all)
- print "\n%s:\n" % (i+1) + "\n".join([": ".join(pair) for pair in sample])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement