-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
61 lines (51 loc) · 2.06 KB
/
logic.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
59
60
61
from collections import deque
from utill.my_random import secure_random
import random
def distribute_people(total_people, num_teams):
people_per_team = total_people // num_teams
remainder = total_people % num_teams
team_distribution = []
for i in range(num_teams):
if i < remainder:
team_distribution.append(people_per_team + 1)
else:
team_distribution.append(people_per_team)
return team_distribution
def rfs(lists: dict, num_teams: int, now_team: int = 0):
key = lists.keys()
key = list(key)
queue = deque()
visited = {i:False for i in key}
team_list = [[] for i in range(num_teams)]
team_distribution = distribute_people(len(key), num_teams)
def now_team_update():
nonlocal now_team
now_team = secure_random(0, num_teams - 1)
while len(team_list[now_team]) >= team_distribution[now_team]:
now_team = (now_team + 1) % num_teams
def append_to_team(i):
nonlocal now_team
if len(team_list[now_team]) < team_distribution[now_team]:
team_list[now_team].append(i)
else:
now_team_update()
team_list[now_team].append(i)
while sum(visited.values()) < len(visited):
if len(queue) == 0:
start = random.choice([i for i in key if visited[i] == False])
queue.append(start)
visited[start] = True
append_to_team(start)
else:
now = queue.popleft()
for i in random.sample(lists[now], min(secure_random(0, len(lists[now]))+1 # if list is not empty
, len(lists[now]))): # if list is empty
# for i in lists[now][:secure_random(0, len(lists[now]))+1]: # Priority can be applied
if visited[i] == False:
queue.append(i)
visited[i] = True
append_to_team(i)
team_list.sort()
for i in range(len(team_list)):
team_list[i].sort()
return team_list