Skip to content

Instantly share code, notes, and snippets.

@jakedt
Last active December 2, 2021 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakedt/0cecf9279b7b99231e0b2cbe6b26e2fc to your computer and use it in GitHub Desktop.
Save jakedt/0cecf9279b7b99231e0b2cbe6b26e2fc to your computer and use it in GitHub Desktop.
Secret Santa random name picker
import random
KIDS = [
{"Abby", "AJ"},
{"Del"},
{"Miles"},
{"Xiaoxu", "Cosie"},
{"Sebastian", "Oliver"},
{"Lilly", "Quinn"},
]
ADULTS = [
{"Jake", "Melissa"},
{"Amy", "Colin"},
{"Dave", "Danielle"},
{"Vivian", "Ling"},
{"Eric", "Masha"},
{"Amber", "Patrick"},
]
def generate_random(list_of_groups: list[set[str]]) -> list[tuple[str, str]]:
participants: list[str] = []
for group in list_of_groups:
for name in group:
participants.append(name)
assignments = participants.copy()
random.shuffle(assignments)
return list(zip(participants, assignments))
def check(assignments: list[tuple[str, str]], list_of_groups: list[set[str]]) -> bool:
for giver, recipient in assignments:
for group in list_of_groups:
if giver in group and recipient in group:
print(f"rejecting: {giver} -> {recipient}")
return False
return True
if __name__ == '__main__':
candidate = generate_random(KIDS)
while not check(candidate, KIDS):
candidate = generate_random(KIDS)
for giver, recipient in candidate:
print(f"{giver} -> {recipient}")
candidate = generate_random(ADULTS)
while not check(candidate, ADULTS):
candidate = generate_random(ADULTS)
for giver, recipient in candidate:
print(f"{giver} -> {recipient}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment