Skip to content

Commit 3c32049

Browse files
committed
Removed input requirement, and explained the partner.setter effect a bit more
1 parent 8fc111e commit 3c32049

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

contents/stable_marriage_problem/code/python/stable_marriage.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
# Submitted by Marius Becker
22
# Updated by Amaras
33

4-
import sys
4+
55
from random import shuffle
66
from copy import copy
77
from string import ascii_uppercase, ascii_lowercase
88

99

1010
def main():
11-
# Set this to however many men and women you want
12-
try:
13-
num_pairs = int(sys.argv[1])
14-
except (IndexError, ValueError):
15-
# If you either did not set how many pairs you wanted or you
16-
# did not set it as a number, use default value of 5
17-
num_pairs = 5
18-
19-
# There are only 26 possible names for each sex
20-
if num_pairs > 26:
21-
print("You can't have more than 26 pairs.")
22-
return
11+
# Set this to however many men and women you want, up to 26
12+
num_pairs = 5
2313

2414
# Create all Person objects
2515
men = [Person(name) for name in ascii_uppercase[:num_pairs]]
@@ -108,15 +98,16 @@ def pick_preferred(self):
10898
self.partner = person
10999
break
110100

111-
# Rejected candidates don't get a second chance. :(
101+
# Rejected candidates don't get a second chance
112102
self.candidates.clear()
113103

114104
@property
115105
def partner(self):
116106
return self._partner
117107

118-
# This allows one to change both self.partner and person.partner
119-
# with the simple call: "self.partner = person"
108+
# The call self.partner = person sets self._partner as person
109+
# However, since engagement is symmetrical, self._partner._partner
110+
# (which is then person._partner) also needs to be set to self
120111
@partner.setter
121112
def partner(self, person):
122113
"""Set a person as the new partner and sets the partner of that

0 commit comments

Comments
 (0)