-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.js
119 lines (87 loc) · 3.25 KB
/
algorithm.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function pairUpBuddies(datesAndMembersList) {
let copyList = [...datesAndMembersList];
let theRestOldStudents = [];
// Filter out dates with no members
copyList = copyList.filter(([date, members]) => members.length > 0);
// Sort datesAndMembersList by the number of members in descending order
copyList.sort((a, b) => b[1].length - a[1].length);
while (copyList.length !== 0) {
let datesAndMembers = copyList.pop();
let date = datesAndMembers[0];
let members = datesAndMembers[1];
// console.log("members: " + members);
let newStudent = [];
let oldStudent = [];
for (let name of members) {
if (name === '') continue;
if (membersInCntTable[name] <= 2) {
if (membersSemesters[name] <= 2) {
newStudent.push(name);
} else {
oldStudent.push(name);
}
}
}
for (let buddy1 of newStudent) {
let buddy2 = null;
if (oldStudent.length !== 0) buddy2 = oldStudent.pop();
// Initializing buddies[date] if it doesn't exist
if (!(date in buddies)) {
buddies[date] = [];
}
if (membersInCntTable[buddy1] <= 2 && membersInCntTable[buddy2] <= 2)
buddies[date].push({ [buddy1]: buddy2 });
else if (membersInCntTable[buddy1] <= 2 && membersInCntTable[buddy2] === null)
buddies[date].push({ [buddy1]: null})
addHisDays(buddy1);
if (buddy2 !== null) addHisDays(buddy2);
}
if (oldStudent.length !== 0) {
theRestOldStudents.push([date, [...oldStudent]]);
}
pairTheRest(theRestOldStudents);
}
}
function pairTheRest(oldStudents) {
oldStudents.reverse();
while (oldStudents.length !== 0) {
let datesAndMembers = oldStudents.pop();
let date = datesAndMembers[0];
let members = datesAndMembers[1];
while (members.length >= 2) {
let buddy1 = members.shift();
let buddy2 = members.pop();
if (membersInCntTable[buddy1] > 2 && membersInCntTable[buddy2] > 2) {
continue;
}
if (!(date in buddies)) {
buddies[date] = [];
}
if (membersInCntTable[buddy1] > 2) {
buddies[date].push({[buddy2]: null});
addHisDays(buddy1);
continue;
}
if (membersInCntTable[buddy2] > 2) {
buddies[date].push({[buddy1]: null});
addHisDays(buddy2);
continue;
}
if (membersInCntTable[buddy1] <= 2 && membersInCntTable[buddy2] <= 2) {
buddies[date].push({ [buddy1]: buddy2 });
addHisDays(buddy1);
addHisDays(buddy2);
}
}
if (members.length === 1) {
let buddy = members.pop();
if (membersInCntTable[buddy] <= 2) {
if (!(date in buddies)) {
buddies[date] = [];
}
buddies[date].push({ [buddy]: null });
addHisDays(buddy);
}
}
}
}