-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctional.py
56 lines (48 loc) · 1.85 KB
/
functional.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
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']
}
l1 = list(Men_Pref.keys())
l2 = list(Women_Pref.keys())
Men_Free = l1
Women_Free = l2
def matches(list_1):
matches = {}
for i in list_1:
matches[i] = ''
return matches
Matches = matches(l1)
key_list = list(Matches.keys())
def start_match():
while len(Men_Free) > 0:
for i in key_list:
match_algo(i)
def match_algo(man):
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))
return
print('\n \n \n ')
print('Stable Matching Finished ! Happy engagement !')
for man in Matches.keys():
print('{} is engaged to {} !'.format(man, Matches[man]))