Skip to content

Instantly share code, notes, and snippets.

@jakedt
Last active Dec 2, 2021
Embed
What would you like to do?
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