-
Notifications
You must be signed in to change notification settings - Fork 0
/
BM.py
54 lines (39 loc) · 1.58 KB
/
BM.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
# Boston Mechanism/Immediate Acceptance: [Student], [School] -> {School -> [Student]}
def Immediate_Acceptance(students, schools, verbose=False):
counter = 1
unassigned = set(students)
while len(unassigned) > 0:
direct_rejection = set()
for student in unassigned:
if len(schools[student.cur_top_pref()].held) >= schools[student.cur_top_pref()].capacity:
direct_rejection.add(student)
for student in unassigned :
if student not in direct_rejection:
schools[student.cur_top_pref()].held.add(student)
unassigned = direct_rejection
if verbose:
app = ""
for school in schools:
app = app + "{"
for student in students:
if student.cur_top_pref() == school.id:
app = app + str(student.id) + ","
if app[-1] == ",":
app = app[:-1] + "} | "
else:
app = app + "} | "
for school in schools:
unassigned |= school.reject()
if verbose:
temp = ""
for school in schools:
temp = temp + str(school.held) + " | "
for student in unassigned:
student.rejections += 1
if verbose:
print('Step: ' + str(counter) + '\n' + app + '\n' + temp)
counter += 1
marriage = list()
for student in students:
marriage.append(student.prefList[student.rejections])
return marriage