feat: implement and document participant matching

This commit is contained in:
Shannon Sterz 2018-09-08 14:08:49 +00:00
commit 6066a150ea

37
wichteln.py Normal file
View file

@ -0,0 +1,37 @@
import json
import random
# load and prepare a list containing all participants
with open('participants.json', 'r') as pFile:
participants = json.load(pFile)
# create a copy of the list to make choosing a partner easier
copy = list(enumerate(participants[:]))
# choose a partner for each participant
for i in range(len(participants)):
# if the last participant has only themselves left to choose, make them
# switch partners with another random participant
if len(copy) == 1 and participants[i] == copy[0][1]:
current = participants[i]
participants.remove(current)
partner = random.choice(participants)
current['partner'] = partner['partner']
partner['partner'] = current['name'] + '' + current['mail']
participants.append(current)
break
# otherwise choose a random partner for each participant
else:
partner = random.choice(copy)
while partner[0] == i:
partner = random.choice(copy)
participants[i]['partner'] = partner[1]['name']+''+partner[1]['mail']
copy.remove(partner)
print(participants)