Skip to content

Instantly share code, notes, and snippets.

Created November 23, 2014 19:25
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 anonymous/d1d5aa99efd263597206 to your computer and use it in GitHub Desktop.
Save anonymous/d1d5aa99efd263597206 to your computer and use it in GitHub Desktop.
Secret Santa
#!/usr/bin/env python3
from random import shuffle
people = [
"Jānis",
"Ilze",
"Kristaps",
"Elīza",
"Alberts",
"Anna",
"Ēvalds",
"Lāsma",
"Imants",
"Alise",
"Roberts",
"Emīlija",
"Mārtiņš",
"Līga",
"Miķelis"
]
forbiddenPairs = {
("Jānis", "Ilze"),
("Imants", "Emīlija"),
("Ēvalds", "Līga"),
}
givers = people[:]
recipients = people[:]
shuffle(recipients)
def isValidPair(giver, recipient):
if (
giver == recipient or
(giver, recipient) in forbiddenPairs or
(recipient, giver) in forbiddenPairs
):
return False
else:
return True
def pairUp(originalGivers, originalRecipients, attempts = 50):
givers = originalGivers[:]
recipients = originalRecipients[:]
failureCounter = 0
pairs = []
while givers:
giver = givers[0]
givers.remove(giver)
while True:
recipient = recipients[0]
recipients.remove(recipient)
if isValidPair(giver, recipient):
pairs += [(giver, recipient)]
failureCounter = 0
break
elif failureCounter > len(recipients):
pairs = None
givers = []
recipients = []
break;
else:
failureCounter += 1
recipients += [recipient]
if pairs:
return pairs
elif attempts > 0:
shuffle(originalRecipients)
return pairUp(originalGivers, originalRecipients, attempts - 1)
else:
return None
if __name__ == '__main__':
pairs = pairUp(givers, recipients)
if pairs:
for (giver, recipient) in pairs:
print("{} dāvina dāvanu {}".format(giver, recipient))
else:
print("Neizdevās sapārot davinātājus.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment