commit 6066a150ea85519421b3fc10b0c14738381a60c9 Author: Stefan Sterz Date: Sat Sep 8 14:08:49 2018 +0000 feat: implement and document participant matching diff --git a/wichteln.py b/wichteln.py new file mode 100644 index 0000000..50f61cb --- /dev/null +++ b/wichteln.py @@ -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) +