File tree 1 file changed +9
-5
lines changed
contents/stable_marriage_problem/code/python
1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -30,13 +30,13 @@ def main():
30
30
# Print preferences and the result
31
31
print ('Preferences of the men:' )
32
32
for man in men :
33
- print (f' { man . name } : { ", " . join (( p . name for p in man . preference )) } ' )
33
+ print (man )
34
34
35
35
print ()
36
36
37
37
print ('Preferences of the women:' )
38
38
for woman in women :
39
- print (f' { woman . name } : { ", " . join (( p . name for p in woman . preference )) } ' )
39
+ print (woman )
40
40
41
41
print ('\n ' )
42
42
@@ -75,11 +75,11 @@ def __init__(self, name):
75
75
@property
76
76
def next_choice (self ):
77
77
"""Return the next person in the own preference list"""
78
- if self .pref_index >= len (self .preference ):
78
+ try :
79
+ return self .preference [self .pref_index ]
80
+ except IndexError :
79
81
return None
80
82
81
- return self .preference [self .pref_index ]
82
-
83
83
def propose_to_next (self ):
84
84
"""Propose to the next person in the own preference list"""
85
85
person = self .next_choice
@@ -130,6 +130,10 @@ def has_partner(self):
130
130
"""Determine whether this person currently has a partner or not."""
131
131
return self .partner is not None
132
132
133
+ # This allows the preferences to be printed more elegantly
134
+ def __str__ (self ):
135
+ return f'{ self .name } : { ", " .join (p .name for p in self .preference )} '
136
+
133
137
134
138
if __name__ == '__main__' :
135
139
main ()
You can’t perform that action at this time.
0 commit comments