Created
April 3, 2012 22:37
-
-
Save yuriyzubarev/2296010 to your computer and use it in GitHub Desktop.
Given a list of people, pick secret santa for everyone. People form the same family shouldn't be santas for each other.
This file contains 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
def parse(people): | |
""" | |
returns a list of tuples; first element in tuple - last name; second element - a dict | |
""" | |
people_dicts = [dict(zip(("first", "last", "email"), person.split())) for person in people.splitlines()] | |
return [(d["last"], d) for d in people_dicts] | |
def santafy(l): | |
def index(i): | |
return i % len(l) | |
def swap(x, y): | |
l[x], l[y] = l[y], l[x] | |
for x in range(len(l)): | |
if l[index(x)][0] == l[index(x + 1)][0]: | |
for xx in range(len(l)): | |
if l[index(x + 1 + xx)][0] != l[index(x + 1 + xx + 1)][0]: | |
swap(index(x + 1 + xx), index(x + 1 + xx + 1)) | |
return santafy(l) | |
return l | |
def report(l): | |
def index(i): | |
return i % len(l) | |
for x in range(len(l)): | |
print "{0} is Santa for {1} {2}".format(l[index(x)][1]["email"], l[index(x+1)][1]["first"], l[index(x+1)][1]["last"]) | |
report(santafy(parse("""Luke Skywalker <luke@theforce.net> | |
Leia Skywalker <leia@therebellion.org> | |
Harry Potter <harry@hogwarts.com> | |
Lily Potter <lily@magic.org> | |
Bruce Wayne <bruce@imbatman.com> | |
Sarah Connor <sarah@terminator.com> | |
John Connor <john@skynet.com>"""))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment