-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (46 loc) · 1.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import random
import copy
token = 'name'
group = 'family'
def read_tokens():
groups = {}
with open("data/sample.json") as file:
data = json.loads(file.read())
for i in data:
groups.setdefault(i[group], []).append(i[token])
return groups
def draw_assignments(groups):
assignments = []
senders = copy.deepcopy(groups)
recipients = copy.deepcopy(groups)
while senders:
# Choose senders and get valid targets for senders
sender_group_name, sender_members = senders.popitem()
targets = copy.deepcopy(recipients)
if sender_group_name in targets:
targets.pop(sender_group_name)
# Assign recievers randomly from valid targets
for person in sender_members:
target_group_key, target_group_values = random.choice(list(targets.items()))
target_member_index = random.randrange(len(target_group_values))
target_member = targets[target_group_key].pop(target_member_index)
assignments.append(
{
'sender': person,
'recipient': target_member
}
)
# Remove recipient from original recipient set
recipients[target_group_key].pop(target_member_index)
# Remove groups as necessary
if len(recipients[target_group_key]) == 0:
recipients.pop(target_group_key)
if len(targets[target_group_key]) == 0:
targets.pop(target_group_key)
return assignments
if __name__ == '__main__':
groups = read_tokens()
assignments = draw_assignments(groups)
with open('output/assignments.json', 'w') as output:
output.write(json.dumps(assignments))