-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stable_Matching.py
61 lines (51 loc) · 2.06 KB
/
Stable_Matching.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
# Initialization
Men = ['Mike', 'Harvey', 'Louis', 'Logan']
Women = ['Rachel', 'Donna', 'Katrina', 'Sheila']
# Preferences
Men_Pref = { # indicates the preferences of the men
'Mike': ['Rachel', 'Katrina', 'Donna', 'Sheila'],
'Harvey': ['Donna', 'Katrina', 'Rachel', 'Sheila'],
'Louis': ['Sheila', 'Donna', 'Katrina', 'Rachel'],
'Logan': ['Rachel', 'Katrina', 'Donna', 'Sheila']
}
Women_Pref = { # indicates the preferences of the women
'Rachel': ['Mike', 'Logan', 'Harvey', 'Louis'],
'Donna': ['Harvey', 'Louis', 'Mike', 'Logan'],
'Katrina': ['Mike', 'Harvey', 'Louis', 'Logan'],
'Sheila': ['Louis', 'Logan', 'Harvey', 'Mike']
}
def main():
Men_Free = list(Men)
Women_Free = list(Women)
# Part 3: Proposal
Matches = {
'Mike': '',
'Harvey': '',
'Louis': '',
'Logan': ''
}
key_list = list(Matches.keys())
# the algorithm
while len(Men_Free) > 0:
for man in key_list:
for woman in Men_Pref[man]:
if woman not in list(Matches.values()):
Matches[man] = woman
Men_Free.remove(man)
print('{} is no longer a free man and is tentatively engaged to {} !'.format(man, woman))
break
elif woman in list(Matches.values()):
current_suitor = list(Matches.keys())[list(Matches.values()).index(woman)]
w_list = Women_Pref.get(woman)
if w_list.index(man) < w_list.index(current_suitor):
Matches[man] = woman
Men_Free.remove(man)
Matches[current_suitor] = ''
Men_Free.append(current_suitor)
print('{} was earlier engaged to {} but now is engaged to {}! '.format(woman, current_suitor, man))
print('\n ')
print('Stable Matching Finished ! Happy engagement !')
for man in Matches.keys():
print('{} is engaged to {} !'.format(man, Matches[man]))
if __name__ == "__main__":
main()