# Re: Preferable Pairs (#165)

**URL:** <https://www.ruby-forum.com/t/re-preferable-pairs-165/139877>\
**Category:** Ruby\
**Created:** [June 8, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/re-preferable-pairs-165/139877 "2008-06-08T16:52:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jameskelly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jameskelly](https://www.ruby-forum.com/u/jameskelly)\
**Post date:** [June 8, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/re-preferable-pairs-165/139877/1 "2008-06-08T16:52:08Z")

</div>

This sounds a lot like the Stable Marriage Problem.

----- Original Message ----  
From: Matthew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, June 6, 2008 12:43:43 PM  
Subject: [QUIZ] Preferable Pairs (#165)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q. 2:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Q. 2 by submitting ideas as often as you can! (A  
permanent, new website is in the works for Ruby Q. 2. Until then,  
please visit the temporary website at

3. Enjoy!  
Suggestion: A [QUIZ] in the subject of emails about the problem  
helps everyone on Ruby T. follow the discussion. Please reply to  
the original quiz message, if you can.  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Preferable Pairs (#156)

Imagine a pairs tournament: a competition where every player partners up  
with another for the duration of the tournament. Everyone plays in a  
pair  
and wins or loses in a pair.

But everyone arrives individually, and only pairs up at the start. Every  
player has preferences for a partner; your task is to try and optimize  
those  
preferences as best as possible.

The input is a series of lines containing names, such as:

```
David: Helen Vicki Joseph
Helen: David Vicki Joseph
Joseph: Vicki Helen David
Vicki: David Helen Joseph

```

Each line represents the preferences of the first named player (i.e.  
before  
the colon). That player’s preferred parters are listed after the colon,  
in  
order of preference (i.e. most preferred first, least preferred last).

The output of your code should be the pairings, one pair per line. For  
the  
example above, your output should look like this:

```
David Helen
Joseph Vicki

```

If an odd number of people were provided at the start, then one person  
will  
be left out. That person’s name should be output alone on the last line.

What determines the best pairing? A score will be calculated for your  
output  
against the input. For each player, the partner is found in the player’s  
ordered list, as an index. So, for the above example:

```
David: 0
Helen: 0
Joseph: 0
Vicki: 2

```

Each individual’s score is then squared, then all are added together.  
Here,  
the final score is 4. The _lower_ your score, the better the match.
