-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
207 lines (132 loc) · 4.95 KB
/
Copy pathscript.js
File metadata and controls
207 lines (132 loc) · 4.95 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
window.onload = function () {
console.log(makePairs(sortBucket([4, 4, 1, 2]), 6));
}
var rowList = [];
function sortBucket(people) {
bucketList = [];
bucketList[0] = 0;
bucketList[1] = 0;
bucketList[2] = 0;
bucketList[3] = 0;
bucketList[4] = 0;
bucketList[5] = 0;
bucketList[6] = 0;
for (let i = 0; i < people.length; i++) {
bucketList[people[i]]++;
}
return bucketList;
}
function makePairs(bucketList, rowCapacity) {
for (let i = bucketList.length - 1; i > 0; i--) {
//if bucket is empty
if (bucketList[i] == 0) {
continue;
}
//if it is last or last-1 bucket
if (i == rowCapacity) {
// push row
for (let j = 0; j < bucketList[i]; j++) {
rowList.push(i);
}
bucketList[i] = 0;
continue;
}
if (i == rowCapacity - 1) {
//push row and one empty seat
for (let j = 0; j < bucketList[i]; j++) {
rowList.push(i);
rowList.push('0');
}
bucketList[i] = 0;
continue;
}
// for pairs that can accomplish each other
if (i >= (bucketList.length - 1) / 2) {
//push it with its accompaniment in list of rows
let fullRows;
if (bucketList[i] <= bucketList[rowCapacity - (i + 1)]) {
fullRows = bucketList[i];
} else {
fullRows = bucketList[rowCapacity - (i + 1)];
}
for (let j = 0; j < fullRows; j++) {
rowList.push(i);
rowList.push('0');
rowList.push(rowCapacity - (i + 1));
}
bucketList[rowCapacity - (i + 1)] -= fullRows;
bucketList[i] -= fullRows;
// bucket size is more than its accompaniment
// need to split next value into 2
if (bucketList[i] - bucketList[rowCapacity - (i + 1)] > 0) {
//list all buckets from accompaniment to current bucket (for ex. 1-4 if row size is 6)
let isAll = true;
for (let j = rowCapacity - (i + 1); j < i; j++) {
console.log("j = " + j);
console.log("i = " + i);
if (bucketList[i] == 0) {
break;
}
//if current value is 0 skip
if (j == 0) {
continue;
}
// if there is accompaniment then input it
if (j == rowCapacity - (i + 1) && bucketList[j] != 0) {
fullRows = 0;
if (bucketList[i] <= bucketList[j]) {
fullRows = bucketList[i];
} else {
fullRows = bucketList[j];
}
for (let j = 0; j < fullRows; j++) {
rowList.push(i);
rowList.push('0');
rowList.push(j);
}
bucketList[j] -= fullRows;
bucketList[i] -= fullRows;
continue
}
// if all values before big bucket are 0 break the loop
isAll = true;
for (let x = rowCapacity - (i + 1); x < i; x++) {
if (bucketList[x] != 0) {
isAll = false;
}
}
if(isAll){break;}
// divide value into accomponiment and another value
rowList.push(i);
rowList.push('0');
rowList.push(rowCapacity - (i + 1));
bucketList[j - (rowCapacity - (i + 1))]++;
bucketList[j]--;
j = rowCapacity - (i + 1);
}
if (bucketList[i] != 0) {
for (let k = 0; k < bucketList[i]; k++) {
rowList.push(i);
rowList.push('0');
}
bucketList[i] = 0;
}
}
}
console.log(bucketList);
}
return rowList;
}
// function makePairs(bucketList, correspondingElement, lastElementOrder, rowCapacity, pairList){
// if(lastElementOrder == 0){
// return pairList;
// } else {
// if(bucketList[lastElementOrder] != 0){
// if(bucketList[rowCapacity - bucketList[lastElementOrder]] == ){
// }
// bucketList[lastElementOrder] += bucketList[rowCapacity - bucketList[lastElementOrder]];
// } else {
// makePairs(bucketList, correspondingElement + 1, lastElementOrder - 1, rowCapacity, pairList);
// }
// }
// }