PPairCrest
Workflows

Coding workflows · Stable preference matching

03
Built around your work

A concrete starting point

Match two groups of at most 20 members from complete strict preference lists. Test empty groups and competing choices, rejecting repeated or missing preferences and checking that no blocking pair remains.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Stable preference matching

A concrete starting point
from collections import deque
def stable_pairs(proposers, receivers):
    if type(proposers) not in (list, tuple) or type(receivers) not in (list, tuple):
        raise ValueError("preference tables must be lists or tuples")
    n = len(proposers)
    if n > 20 or len(receivers) != n: raise ValueError("equal sizes up to 20")
    for rows in (proposers, receivers):
        for row in rows:
            if type(row) not in (list, tuple) or len(row) != n or any(type(v) is not int for v in row) or set(row) != set(range(n)):
                raise ValueError("complete strict preferences")
    ranks = [{v:i for i,v in enumerate(row)} for row in receivers]
    free, next_choice = deque(range(n)), [0]*n
    owner, partner = [None]*n, [None]*n
    while free:
        p = free.popleft()
        r = proposers[p][next_choice[p]]
        next_choice[p] += 1
        old = owner[r]
        if old is None or ranks[r][p] < ranks[r][old]:
            owner[r], partner[p] = p, r
            if old is not None: partner[old] = None; free.append(old)
        else: free.append(p)
    return partner

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗