Skip to content

Instantly share code, notes, and snippets.

@Mikael-Lovqvist
Created December 14, 2022 16:30
Show Gist options
  • Save Mikael-Lovqvist/80b8f8b23845ad8922fbffbb44de1302 to your computer and use it in GitHub Desktop.
Save Mikael-Lovqvist/80b8f8b23845ad8922fbffbb44de1302 to your computer and use it in GitHub Desktop.
Secret Santa matching script
import random
participants = '''
Curie
Einstein
Faraday
Maxwell
Newton
Tesla
'''
class BadLuckException(Exception):
pass
p_list = tuple(e.strip() for e in participants.split('\n') if e.strip())
def draw(p_list):
remaining = set(p_list)
result = dict()
for name in p_list:
options = remaining - {name}
if not options:
raise BadLuckException('Drawing made someone their own secret Santa')
recipient = random.choice(tuple(options))
remaining.remove(recipient)
result[name] = recipient
return result
while True:
try:
result = draw(p_list)
break
except BadLuckException:
print('Retrying drawing due to bad luck...')
for santa, recipient in sorted(result.items()):
print(f'{santa} → {recipient}')
# Example output
# Curie → Tesla
# Einstein → Maxwell
# Faraday → Curie
# Maxwell → Newton
# Newton → Einstein
# Tesla → Faraday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment