-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_paires.py
59 lines (38 loc) · 1.24 KB
/
generate_paires.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
58
import json
import random
from multiprocessing import Pool, cpu_count
from employee import Employee
def load_data(path: str) -> list:
employee_lst = []
try:
with open(path,'r') as f:
json_data = json.load(f)
for e in json_data:
employee_lst.append(Employee(**e))
return employee_lst
except FileNotFoundError:
print(f"File {path} not found")
exit(1)
def remove_duplicates(employee_lst: list) -> list:
seen = set() # employees are I see before
unique_employee_lst = []
for e in employee_lst:
if e not in seen:
seen.add(e)
unique_employee_lst.append(e)
return unique_employee_lst
def create_couples(employee_lst: list) -> tuple:
random.shuffle(employee_lst) # Shuffle the list
n = len(employee_lst)
'''[1,2,3]
[ (1,2),(2,3), (3, 1) ]
index -> (0,1), (1,2), (2,0)
i=0 -> i + 1 % n = 1 % 3 = 1
i=1 -> i + 1 % n = 2 % 3 = 2
i=2 -> i + 1 % n = 3 % 3 = 0
'''
return [(employee_lst[i].get_name(), employee_lst[(i+1)%n].get_name()) for i in range(n)]
if __name__ =="__main__":
result = create_couples(remove_duplicates(load_data("data.json")))
# print(len(result))
print(result)