Skip to content

Instantly share code, notes, and snippets.

@andrewtremblay
Last active December 5, 2020 21:00
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 andrewtremblay/5fd8829fd965d61c592130dc3695a145 to your computer and use it in GitHub Desktop.
Save andrewtremblay/5fd8829fd965d61c592130dc3695a145 to your computer and use it in GitHub Desktop.
Secret Santa Source Code
import csv
import random
input_spreadsheet_name = 'secret_santas_input.csv'
secret_spreadsheet_name = 'secret_santa_assigments.csv'
# recursive match making
def make_matches(remaining_people, next_santa=None, original_santa=None, processed_people={}):
if len(remaining_people) is 0:
return processed_people
if next_santa is None:
next_santa = random.choice(remaining_people)
remaining_people.remove(next_santa)
if original_santa is None:
original_santa = next_santa
if len(remaining_people) is 0:
processed_people[next_santa] = original_santa
return processed_people
# randomly select a recipient from remaining pool
recipient = random.choice(remaining_people)
processed_people[next_santa] = recipient
# make the receipient the next santa
return make_matches(remaining_people, recipient, original_santa, processed_people)
with open(input_spreadsheet_name) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
unique_rows = 0
duplicates = []
people = {}
for row in csv_reader:
if line_count == 0:
print(f'processing columns {", ".join(row)}')
elif people.get(row[1], None) is not None:
duplicates.append(row[1])
else:
unique_rows += 1
people[row[1]] = {'email': row[1], 'address': row[2], 'name': row[3] or "Anonymous", 'gender': (row[4] or "none given"), 'interests': (
row[5] or "none given"), 'allergies': (row[6] or "none given")}
line_count += 1
# assign one person to another randomly
emails = [k for k in people.keys()]
processed_emails = []
matches = make_matches(emails)
print(f'{line_count} lines in the spreadsheet. {unique_rows} unique emails (duplicates: {duplicates}). matches: {len(matches.keys())}')
written_line_count = 0
with open(secret_spreadsheet_name, 'w', newline='') as csvfile:
fieldnames = ['santa_email', 'recipient_email', 'recipient_name', 'recipient_gender',
'recipient_address', 'recipient_interests', 'recipient_allergies']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for match in matches.keys():
recipient = people[matches[match]]
writer.writerow({'santa_email': match,
'recipient_email': recipient['email'],
'recipient_name': recipient['name'],
'recipient_gender': recipient['gender'],
'recipient_address': recipient['address'],
'recipient_interests': recipient['interests'],
'recipient_allergies': recipient['allergies']})
written_line_count += 1
print(f'{written_line_count} lines written to secret spreadsheet {secret_spreadsheet_name}')
santa_email recipient_email recipient_name recipient_gender recipient_address recipient_interests recipient_allergies
exampleemail3@gmail.com exampleemail2@gmail.com Person 2 none given 432 fake street Many other interests none given
exampleemail2@gmail.com exampleemail4@gmail.com Anonymous Male 999 fake street no interests none given
exampleemail4@gmail.com exampleemail1@gmail.com Person 1 none given 123 fake street Many interests none given
exampleemail1@gmail.com exampleemail3@gmail.com Anonymous Female 567 fake street Many varying interests none given
Timestamp Your email Mailing Address Your Name Gender Interests Allergies
11/28/2020 15:29:55 exampleemail1@gmail.com 123 fake street Person 1 Many interests
11/28/2020 15:29:55 exampleemail2@gmail.com 432 fake street Person 2 Many other interests
11/28/2020 15:29:55 exampleemail3@gmail.com 567 fake street Female Many varying interests
11/28/2020 15:29:55 exampleemail4@gmail.com 999 fake street Male no interests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment