From 6066a150ea85519421b3fc10b0c14738381a60c9 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sat, 8 Sep 2018 14:08:49 +0000 Subject: [PATCH] feat: implement and document participant matching --- wichteln.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 wichteln.py 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) +