I’m developing the necessary logic in Python for organizing a Secret Santa event, and I’d like to do something like this:
Let’s imagine 3 participants:
Pepe
Martin
Antonio
First Case
Now, with these 3 participants, let’s say we don’t want Pepe to give a gift to Martin, meaning we add an exclusion for Pepe so he won’t gift to Martin.
Pepe exclusions [2]
Martin
Antonio
Note: This doesn’t mean Martin won’t give a gift to Pepe. Pepe won’t give a gift to Martin, but Martin can give a gift to Pepe. It’s important that in this specific case, Pepe ends up giving a gift to Antonio, because if Martin ends up giving a gift to Antonio, Pepe will be left without giving a gift to anyone.
Second Case
Also, the program should consider that exclusions aren’t abusive. For example, this wouldn’t work because two people can’t give a gift to only one person, as there would be one person who wouldn’t receive any gifts.
Pepe exclusions [2]
Martin exclusions [1]
Antonio exclusions [1]
This is the code im triying that work good with 3 participant but not with more
def assign_participants(draw):
participants = list(draw.participants.all())
participants_copy = list(participants)
for participant in participants:
exclusions = Exclusion.objects.filter(participant=participant)
available = [p for p in participants_copy if p != participant and p not in [e.excluded_participant for e in exclusions]]
if not available:
available = [p for p in participants_copy if p != participant]
if available:
assigned_participant = random.choice(available)
else:
print("no participants available")
assignment = Assignment(draw=draw, participant=participant, assigned_participant=assigned_participant)
assignment.save()
participants_copy.remove(assigned_participant)