Skip to content

Implementation Stable marriage problem in PHP #428

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 5 commits into from
Oct 8, 2018
Merged

Implementation Stable marriage problem in PHP #428

merged 5 commits into from
Oct 8, 2018

Conversation

PudottaPommin
Copy link
Member

Worked as intended on provided sample 💃

@june128 june128 added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 3, 2018
@Butt4cak3 Butt4cak3 self-assigned this Oct 4, 2018
@Butt4cak3
Copy link
Contributor

I'd say we deal with #431 before we continue with the other PHP PRs.

removed annotations
changed sprintf to printf
@PudottaPommin
Copy link
Member Author

@Butt4cak3 good to go

Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

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

A couple of issues. I don't think that I'll have to explain too much because we already talked about all of this.

As with the other PR, you should use brackets for every control statement.


public function setPreferences(array $preferences): void
{
shuffle($preferences);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd move this outside the setPreferences method.

}
}

class Man extends Person
Copy link
Contributor

Choose a reason for hiding this comment

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

A few months ago, we had a discussion on Discord about whether we'd actually need 2 subclasses for Person and we agreed that we don't. So I need to you move the subclass code up to the Person class.


function stable_marriage(array $men, array $women): void
{
$smallerGroup = count($men) < count($women) ? $men : $women;
Copy link
Contributor

Choose a reason for hiding this comment

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

Both arrays are the same size by definition. You don't have to check this.

foreach ($women as $woman)
$woman->chooseMatch();

if (empty(array_filter($smallerGroup, function (Person $person) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This loops over all the objects even if the result is already determined. If the first person is single, then there is no need to loop over all the others. I recommend changing this to a foreach loop with a break statement.


} while (true);

foreach ($women as $woman) printf('%s is married to %s%s', $woman, $woman->getMatch(), PHP_EOL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Move the output out of the algorithm's function.

Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

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

One more thing

}
echo PHP_EOL;
stable_marriage($men, $women);

$married = stable_marriage($men, $women);
Copy link
Contributor

@Butt4cak3 Butt4cak3 Oct 8, 2018

Choose a reason for hiding this comment

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

I don't understand why you changed stable_marriage to return. You could just use foreach ($women as $woman) in the output loop. In fact, $women and $married have the exact same contents. That's actually how every other implementation does it.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, you are right. I forgot that those are objects, which goes by reference and treated it like arrays which are copies.

@Butt4cak3 Butt4cak3 merged commit e3d61a8 into algorithm-archivists:master Oct 8, 2018
@PudottaPommin PudottaPommin deleted the php_stable_marriage_problem branch October 14, 2018 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! 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.

3 participants