-
-
Notifications
You must be signed in to change notification settings - Fork 360
Implement Java version of the stable marriage problem. #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
* lists of men and women. | ||
*/ | ||
public static void findStableMarriages(List<Woman> women, List<Man> men) { | ||
boolean keep_going; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going by the Java style guide this should be keepGoing
// Repeat the process if someone is left without a partner. | ||
keep_going = false; | ||
for (Person person : leastCommonGender) { | ||
if (keep_going |= person.isLonely()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like this. First, it uses the less common variant of the OR operator (non short-circuiting). Second, it checks the result of an assignemnt which is probably even worse. You could do this instead:
keepGoing = leastCommonGender.stream().anyMatch(Person::isLonely)
Or alternatively, get rid of keepGoing
altogether:
if (!leastCommonGender.anyMatch(Person::isLonely)) {
break;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I very much agree, that is much cleaner!
|
||
public static void main(String[] args) { | ||
int nPairs = 5; | ||
List<Woman> women = new LinkedList<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should really use ArrayList
in both of these cases. LinkedList
is just a bad data structure in general
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning was that removal from the front of the list would be efficient. But I agree, ArrayList
is probably at least just as good.
class Person { | ||
private final String name; | ||
protected Person mate; | ||
protected List<Person> preferedMates; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefered -> preferred
|
||
public void receiveOptions(List<? extends Person> mates) { | ||
// Preferences are subjective. | ||
preferedMates = new LinkedList<>(mates); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, use an ArrayList here
} | ||
|
||
class Woman extends Person { | ||
private List<Man> suitors = new LinkedList<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ArrayList
@Gustorn Have you had time to look over the changes? Just realized this has been sitting untouched for some weeks now. |
I reviewed the changes you made and found everything like @Gustorn requested it. I also went ahead and checked that everything displays properly on the website. I'll go ahead and merge this. |
It's 24 days old and everything has been changed as requested
No description provided.