回答編集履歴
3
追記
test
CHANGED
@@ -111,3 +111,187 @@
|
|
111
111
|
print(ret)# [(1, 4), (2, 3), (3, 2), (4, 1), (5, 5), (6, 6)]
|
112
112
|
|
113
113
|
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
#追記2
|
118
|
+
|
119
|
+
処理を見直しました。
|
120
|
+
|
121
|
+
これならおそらくうまく割り振られると思います。
|
122
|
+
|
123
|
+
```Python
|
124
|
+
|
125
|
+
import random
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
seat_1 = [2,3,4,5,6,7,8,12,13,15,19,22,30,31]
|
130
|
+
|
131
|
+
seat_2 = [2,3,4,5,6,7,8,13,15,16,19,20,22,25,26,30,31,36]
|
132
|
+
|
133
|
+
seat_3 = [2,4,6,7,8,14,15,16,20,25,26,30,33,36,37]
|
134
|
+
|
135
|
+
seat_4 = [1,2,4,6,7,8,14,15,16,17,20,23,25,26,28,30,33,36,37]
|
136
|
+
|
137
|
+
seat_5 = [1,2,4,6,7,8,14,16,17,20,23,25,26,28,30,33,36,37]
|
138
|
+
|
139
|
+
seat_6 = [29]
|
140
|
+
|
141
|
+
seat_7 = [2,3,6,7,8,10,15,16,19,22,25,30,31,36]
|
142
|
+
|
143
|
+
seat_8 = [2,6,7,8,10,14,15,16,25,30,33,36,37]
|
144
|
+
|
145
|
+
seat_9 = [1,2,6,7,8,10,14,15,16,17,23,25,28,30,33,36,37]
|
146
|
+
|
147
|
+
seat_10 = [1,2,6,7,8,14,16,17,23,25,28,30,32,33,36,37]
|
148
|
+
|
149
|
+
seat_11 = [1,2,4,6,7,8,14,16,17,18,23,28,30,32,33,34,37]
|
150
|
+
|
151
|
+
seat_12 = [5,6,7,8,12,13,22,30,31]
|
152
|
+
|
153
|
+
seat_13 = [5,6,7,8,13,22,30,31]
|
154
|
+
|
155
|
+
seat_14 = [6,7,8,10,16,22,30,31]
|
156
|
+
|
157
|
+
seat_15 = [6,7,8,10,14,16,21,30,33,37]
|
158
|
+
|
159
|
+
seat_16 = [1,6,7,8,10,14,16,17,21,23,28,30,33,37]
|
160
|
+
|
161
|
+
seat_17 = [1,6,7,8,14,16,17,21,23,28,30,33,37]
|
162
|
+
|
163
|
+
seat_18 = [1,4,6,7,8,14,16,17,18,23,28,30,32,33,34,37]
|
164
|
+
|
165
|
+
seat_19 = [5,6,7,8,12,13,22,30]
|
166
|
+
|
167
|
+
seat_20 = [5,6,7,8,13,22,24,30]
|
168
|
+
|
169
|
+
seat_21 = [6,7,8,10,16,22,24,30]
|
170
|
+
|
171
|
+
seat_22 = [6,7,8,10,14,16,21,30,33,37]
|
172
|
+
|
173
|
+
seat_23 = [1,6,7,8,10,14,16,17,21,23,28,30,33,37]
|
174
|
+
|
175
|
+
seat_24 = [1,6,7,8,14,16,17,21,23,28,30,33,37]
|
176
|
+
|
177
|
+
seat_25 = [1,4,6,7,8,14,16,17,18,23,28,30,32,33,34,37]
|
178
|
+
|
179
|
+
seat_26 = [5,6,7,8,12,13,22,30]
|
180
|
+
|
181
|
+
seat_27 = [5,6,7,8,13,22,24,30]
|
182
|
+
|
183
|
+
seat_28 = [6,7,8,10,16,22,24,30]
|
184
|
+
|
185
|
+
seat_29 = [6,7,8,10,14,16,24,30,33,37]
|
186
|
+
|
187
|
+
seat_30 = [1,6,7,8,10,14,16,17,23,28,30,33,37]
|
188
|
+
|
189
|
+
seat_31 = [1,6,7,8,14,16,17,23,28,30,33,37]
|
190
|
+
|
191
|
+
seat_32 = [1,4,6,7,8,14,16,17,18,23,28,30,32,33,34,37]
|
192
|
+
|
193
|
+
seat_33 = [4,5,6,7,8,13,22,24,27,30]
|
194
|
+
|
195
|
+
seat_34 = [27]
|
196
|
+
|
197
|
+
seat_35 = [11]
|
198
|
+
|
199
|
+
seat_36 = [9]
|
200
|
+
|
201
|
+
seat_37 = [35]
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
seats = [seat_1,seat_2,seat_3,seat_4,seat_5,
|
206
|
+
|
207
|
+
seat_6,seat_7,seat_8,seat_9,seat_10,
|
208
|
+
|
209
|
+
seat_11,seat_12,seat_13,seat_14,seat_15,
|
210
|
+
|
211
|
+
seat_16,seat_17,seat_18,seat_19,seat_20,
|
212
|
+
|
213
|
+
seat_21,seat_22,seat_23,seat_24,seat_25,
|
214
|
+
|
215
|
+
seat_26,seat_27,seat_28,seat_29,seat_30,
|
216
|
+
|
217
|
+
seat_31,seat_32,seat_33,seat_34,seat_35,
|
218
|
+
|
219
|
+
seat_36,seat_37]
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
# (席番号, 候補リスト)に整形
|
224
|
+
|
225
|
+
seats = [(i+1,v) for i,v in enumerate(seats)]
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
ret = []
|
230
|
+
|
231
|
+
while seats:
|
232
|
+
|
233
|
+
for i, seat in enumerate(seats):
|
234
|
+
|
235
|
+
# 候補がいる席に対して抽選
|
236
|
+
|
237
|
+
# (最初は人気がなくて候補がいない席もあることを考慮)
|
238
|
+
|
239
|
+
if len(seat[1]) >= 1:
|
240
|
+
|
241
|
+
seat = seats.pop(i) # popによって今から抽選する席番号は取り除かれる
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
# 抽選して結果を保存
|
246
|
+
|
247
|
+
win = random.choice(seat[1]) # 当選した人番号
|
248
|
+
|
249
|
+
ret.append((seat[0], win)) # (席番号, 人番号)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
# 各席から当選した人を取り除く
|
254
|
+
|
255
|
+
seats = [ (s[0], [p for p in s[1] if p != win]) for s in seats]
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
# 落選した人で、他にどの席も希望していない人は
|
260
|
+
|
261
|
+
# あぶれるので有無を言わせず(ランダムに)別の席の抽選に振り分ける
|
262
|
+
|
263
|
+
loses = [v for v in seat[1] if v != win]
|
264
|
+
|
265
|
+
for lose in loses:
|
266
|
+
|
267
|
+
is_exist = False
|
268
|
+
|
269
|
+
for seat in seats:
|
270
|
+
|
271
|
+
if lose in seat[1]:
|
272
|
+
|
273
|
+
is_exist = True
|
274
|
+
|
275
|
+
break
|
276
|
+
|
277
|
+
if not is_exist:
|
278
|
+
|
279
|
+
seat = random.choice(seats)
|
280
|
+
|
281
|
+
seat[1].append(lose)
|
282
|
+
|
283
|
+
break
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
assert(len(ret) == len(set([v[0] for v in ret]))) # すべての席に割り当てられた
|
288
|
+
|
289
|
+
assert(len(ret) == len(set([v[1] for v in ret]))) # すべての人が割り当てられた
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
ret.sort(key=lambda v:v[0]) # 結果を席番号順に
|
294
|
+
|
295
|
+
print(ret)
|
296
|
+
|
297
|
+
```
|
2
修正
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
# 追記
|
30
30
|
|
31
|
-
いや、
|
31
|
+
いや、以下ではだめですね。
|
32
32
|
|
33
33
|
席1に1と3番、席2と席3に1と2番の人が希望した場合、席1に1番の人が座ると破綻しますね。
|
34
34
|
|
1
追記
test
CHANGED
@@ -26,9 +26,19 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
# 追記
|
30
|
+
|
31
|
+
いや、これではだめですね。
|
32
|
+
|
33
|
+
席1に1と3番、席2と席3に1と2番の人が希望した場合、席1に1番の人が座ると破綻しますね。
|
34
|
+
|
35
|
+
|
36
|
+
|
29
|
-
ちなみに、以下のような抽選方法だと席の数のループだけで結果が得られます。
|
37
|
+
~~ちなみに、以下のような抽選方法だと席の数のループだけで結果が得られます。
|
30
38
|
|
31
39
|
なお、「席1と席2に6番の人だけが応募する」というような、結果が得られない組み合わせは考慮していません。
|
40
|
+
|
41
|
+
~~
|
32
42
|
|
33
43
|
```Phthon
|
34
44
|
|
@@ -53,6 +63,10 @@
|
|
53
63
|
seats = [seat_1,seat_2,seat_3,seat_4,seat_5,
|
54
64
|
|
55
65
|
seat_6]
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
seats = [[1,3],[1,2],[1,2]] # だめなケースを追記
|
56
70
|
|
57
71
|
|
58
72
|
|