-
-
Save eyeseast/729803 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
santa.py | |
Created by Chris Amico on 2010-12-10. | |
Use at your own risk. | |
""" | |
import csv | |
import random | |
import sys | |
import os | |
class Santa(object): | |
""" | |
Given a list of name/email pairs, | |
assign people to give each other | |
gifts. | |
Can also email everyone. | |
>>> from santa import Santa | |
>>> people = ( | |
... ('Chris', '[email protected]'), | |
... ('Dave', '[email protected]'), | |
... ('Vanessa', '[email protected]'), | |
... ) | |
>>> santa = Santa(people) | |
>>> santa.sort() | |
# random results should come back | |
""" | |
def __init__(self, people): | |
self.people = dict(people) | |
self.gifters = set(self.people.keys()) | |
self.getters = set(self.people.keys()) | |
self.results = dict() | |
def sort(self): | |
while self.gifters: | |
giver = random.choice(list(self.gifters)) | |
getter = random.choice(list(self.getters)) | |
if not giver == getter: | |
self.gifters.remove(giver) | |
self.getters.remove(getter) | |
self.results[giver] = getter | |
return self.results | |
def main(): | |
with open(sys.argv[1]) as f: | |
reader = csv.reader(f) | |
people = (row for row in reader) | |
santa = Santa(people) | |
santa.sort() | |
for result in santa.results.items(): | |
print("%s gives to %s" % result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment