37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
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)
|
|
|