Skip to content

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

Merged
merged 2 commits into from
Sep 1, 2018

Conversation

bsamseth
Copy link
Contributor

@bsamseth bsamseth commented Aug 7, 2018

No description provided.

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Aug 8, 2018
zsparal
zsparal previously requested changes Aug 9, 2018
* lists of men and women.
*/
public static void findStableMarriages(List<Woman> women, List<Man> men) {
boolean keep_going;
Copy link
Contributor

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()) {
Copy link
Contributor

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;
}

Copy link
Contributor Author

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<>();
Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor

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);
Copy link
Contributor

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<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ArrayList

@bsamseth
Copy link
Contributor Author

@Gustorn Have you had time to look over the changes? Just realized this has been sitting untouched for some weeks now.

@Butt4cak3
Copy link
Contributor

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.

@Butt4cak3 Butt4cak3 dismissed zsparal’s stale review September 1, 2018 09:56

It's 24 days old and everything has been changed as requested

@Butt4cak3 Butt4cak3 merged commit a335dd3 into algorithm-archivists:master Sep 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants