Stable marriage problem: Difference between revisions
→{{header|J}}: add whitespace, some simplification & rename some nouns and verbs
(→{{header|J}}: add whitespace, some simplification & rename some nouns and verbs) |
|||
Line 363:
)
init=: (,:%)_"0
propose=:
'guy gal'=. y
if. gal e. engaged do.
pref=. gal{
fiance=. engaged i. gal
if. guy <&(pref&i.) fiance do.
end.
else.
engaged=. gal guy} engaged
end.
engaged
)
matchMake=: monad define
'engaged fallback'=. y
whilst. _
for_guy. I. _
next=.
gal=. (<guy,next){
end.
end.
)
checkStable=: monad define
satisfied=. ] >: (<0 1) |: ]
stable=.
if. bad=. 0 e. ,stable do.
smoutput 'Better matches:'
smoutput ((
end.
assert-.bad
)</lang>▼
▲</lang>
For most of this, males and females are both represented by indices. Rows of
The key data structure here (the right argument for <code>
Example use:
<lang>
┌───┬────┬───┬───┬───┬────┬───┬───┬────┬───┐
│abe│bob │col│dan│ed │fred│gav│hal│ian │jon│
Line 429 ⟶ 427:
Stability check:
<lang j> 1 2 A."_1
┌───┬────┬───┬───┬───┬────┬───┬────┬───┬───┐
│abe│bob │col│dan│ed │fred│gav│hal │jon│ian│
Line 435 ⟶ 433:
│ivy│cath│dee│fay│jan│bea │gay│hope│eve│abi│
└───┴────┴───┴───┴───┴────┴───┴────┴───┴───┘
Better matches:
┌───┬────┐
|