Skip to content

Instantly share code, notes, and snippets.

@yuriyzubarev
Created April 3, 2012 22:37
Show Gist options
  • Save yuriyzubarev/2296010 to your computer and use it in GitHub Desktop.
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.
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 <[email protected]>
Leia Skywalker <[email protected]>
Harry Potter <[email protected]>
Lily Potter <[email protected]>
Bruce Wayne <[email protected]>
Sarah Connor <[email protected]>
John Connor <[email protected]>""")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment