-
-
Save jakedt/0cecf9279b7b99231e0b2cbe6b26e2fc to your computer and use it in GitHub Desktop.
Secret Santa random name picker
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
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