Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,74 @@
|
|||
(lib 'hash)
|
||||
;; input data
|
||||
(define M-RANKS
|
||||
'(( abe abi eve cath ivy jan dee fay bea hope gay)
|
||||
( bob cath hope abi dee eve fay bea jan ivy gay)
|
||||
( col hope eve abi dee bea fay ivy gay cath jan)
|
||||
( dan ivy fay dee gay hope eve jan bea cath abi)
|
||||
( ed jan dee bea cath fay eve abi ivy hope gay)
|
||||
( fred bea abi dee gay eve ivy cath jan hope fay)
|
||||
( gav gay eve ivy bea cath abi dee hope jan fay)
|
||||
( hal abi eve hope fay ivy cath jan bea gay dee)
|
||||
( ian hope cath dee gay bea abi fay ivy jan eve)
|
||||
( jon abi fay jan gay eve bea dee cath ivy hope)))
|
||||
|
||||
(define W-RANKS
|
||||
'(( abi bob fred jon gav ian abe dan ed col hal)
|
||||
( bea bob abe col fred gav dan ian ed jon hal)
|
||||
( cath fred bob ed gav hal col ian abe dan jon)
|
||||
( dee fred jon col abe ian hal gav dan bob ed)
|
||||
( eve jon hal fred dan abe gav col ed ian bob)
|
||||
( fay bob abe ed ian jon dan fred gav col hal)
|
||||
( gay jon gav hal fred bob abe col ed dan ian)
|
||||
( hope gav jon bob abe ian dan hal ed col fred)
|
||||
( ivy ian col hal gav fred bob abe ed jon dan)
|
||||
( jan ed hal gav abe bob jon col ian fred dan)))
|
||||
|
||||
;; build preferences hash
|
||||
(define (set-prefs ranks prefs)
|
||||
(for/list ((r ranks))
|
||||
(hash-set prefs (first r) (rest r))
|
||||
(first r)))
|
||||
|
||||
(define (engage m w) (hash-set ENGAGED m w) (hash-set ENGAGED w m) (writeln m w '👫 ))
|
||||
(define (disengage m w) (hash-remove! ENGAGED m ) (hash-remove! ENGAGED w) (writeln '💔 m w))
|
||||
(define (engaged x) (hash-ref ENGAGED x))
|
||||
(define (free? x) (not (engaged x)))
|
||||
(define (free-man men) (for ((man men)) #:break (free? man) => man #f))
|
||||
|
||||
|
||||
(define (prefers? prefs x a b) (member b (member a (hash-ref prefs x))))
|
||||
;; get first choice and remove it from prefs list
|
||||
(define (first-choice prefs m)
|
||||
(define w (first (hash-ref prefs m)))
|
||||
(hash-set prefs m (rest (hash-ref prefs m)))
|
||||
w)
|
||||
|
||||
;; sets ENGAGED couples
|
||||
;; https//en.wikipedia.org/wiki/Stable_marriage_problem
|
||||
|
||||
(define (stableMatching (prefs (make-hash)) (m) (w))
|
||||
(define-global 'ENGAGED (make-hash))
|
||||
(define men (set-prefs M-RANKS prefs))
|
||||
(define women (set-prefs W-RANKS prefs))
|
||||
(while (setv! m (free-man men))
|
||||
(set! w (first-choice prefs m))
|
||||
(if (free? w)
|
||||
(engage m w)
|
||||
(let [(dumped (engaged w))]
|
||||
(when (prefers? prefs w m dumped)
|
||||
(disengage w dumped)
|
||||
(engage w m)))))
|
||||
(hash->list ENGAGED))
|
||||
|
||||
;; input : ENGAGED couples
|
||||
(define (checkStable (prefs (make-hash)))
|
||||
(define men (set-prefs M-RANKS prefs))
|
||||
(define women (set-prefs W-RANKS prefs))
|
||||
(for* [(man men) (woman women)]
|
||||
#:continue (equal? woman (engaged man))
|
||||
(when (and
|
||||
(prefers? prefs man woman (engaged man))
|
||||
(prefers? prefs woman man (engaged woman)))
|
||||
(error 'not-stable (list man woman)))))
|
||||
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
var he_likes = Hash(
|
||||
abe => < abi eve cath ivy jan dee fay bea hope gay >,
|
||||
bob => < cath hope abi dee eve fay bea jan ivy gay >,
|
||||
col => < hope eve abi dee bea fay ivy gay cath jan >,
|
||||
dan => < ivy fay dee gay hope eve jan bea cath abi >,
|
||||
ed => < jan dee bea cath fay eve abi ivy hope gay >,
|
||||
fred => < bea abi dee gay eve ivy cath jan hope fay >,
|
||||
gav => < gay eve ivy bea cath abi dee hope jan fay >,
|
||||
hal => < abi eve hope fay ivy cath jan bea gay dee >,
|
||||
ian => < hope cath dee gay bea abi fay ivy jan eve >,
|
||||
jon => < abi fay jan gay eve bea dee cath ivy hope >,
|
||||
);
|
||||
|
||||
var she_likes = Hash(
|
||||
abi => < bob fred jon gav ian abe dan ed col hal >,
|
||||
bea => < bob abe col fred gav dan ian ed jon hal >,
|
||||
cath => < fred bob ed gav hal col ian abe dan jon >,
|
||||
dee => < fred jon col abe ian hal gav dan bob ed >,
|
||||
eve => < jon hal fred dan abe gav col ed ian bob >,
|
||||
fay => < bob abe ed ian jon dan fred gav col hal >,
|
||||
gay => < jon gav hal fred bob abe col ed dan ian >,
|
||||
hope => < gav jon bob abe ian dan hal ed col fred >,
|
||||
ivy => < ian col hal gav fred bob abe ed jon dan >,
|
||||
jan => < ed hal gav abe bob jon col ian fred dan >,
|
||||
);
|
||||
|
||||
var guys = he_likes.keys;
|
||||
var gals = she_likes.keys;
|
||||
|
||||
var (:fiancé, :fiancée, :proposed);
|
||||
|
||||
func she_prefers (her, hottie) { var a = she_likes{her}; a.index(hottie) < a.index(fiancé{her}) }
|
||||
func he_prefers (him, hottie) { var a = he_likes{him}; a.index(hottie) < a.index(fiancée{him}) }
|
||||
|
||||
func unmatched_guy { guys.first {|k| !defined fiancée{k} } }
|
||||
func preferred_choice(guy) { he_likes{guy}.first {|k| !defined proposed{guy}{k} } }
|
||||
|
||||
func engage(guy, gal) {
|
||||
fiancé{gal} = guy;
|
||||
fiancée{guy} = gal;
|
||||
}
|
||||
|
||||
func match_em {
|
||||
say 'Matchmaking:';
|
||||
while (defined(var guy = unmatched_guy())) {
|
||||
var gal = preferred_choice(guy);
|
||||
proposed{guy}{gal} = '❤';
|
||||
if (!defined fiancé{gal}) {
|
||||
engage(guy, gal);
|
||||
say "\t#{gal} and #{guy}";
|
||||
}
|
||||
elsif (she_prefers(gal, guy)) {
|
||||
var engaged_guy = fiancé{gal};
|
||||
engage(guy, gal);
|
||||
fiancée{engaged_guy} = nil;
|
||||
say "\t#{gal} dumped #{engaged_guy} for #{guy}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func check_stability {
|
||||
var instabilities = gather {
|
||||
guys.each { |m|
|
||||
gals.each { |w|
|
||||
if (he_prefers(m, w) && she_prefers(w, m)) {
|
||||
take "\t#{w} prefers #{m} to #{fiancé{w}} and #{m} prefers #{w} to #{fiancée{m}}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
say 'Stablility:';
|
||||
instabilities.is_empty
|
||||
? say "\t(all marriages stable)"
|
||||
: instabilities.each { |i| say i };
|
||||
}
|
||||
|
||||
func perturb_em {
|
||||
say 'Perturb:';
|
||||
say "\tengage abi with fred and bea with jon";
|
||||
engage('fred', 'abi');
|
||||
engage('jon', 'bea');
|
||||
}
|
||||
|
||||
match_em();
|
||||
check_stability();
|
||||
|
||||
perturb_em();
|
||||
check_stability();
|
||||
138
Task/Stable-marriage-problem/Swift/stable-marriage-problem.swift
Normal file
138
Task/Stable-marriage-problem/Swift/stable-marriage-problem.swift
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
class Person {
|
||||
let name:String
|
||||
var candidateIndex = 0
|
||||
var fiance:Person?
|
||||
var candidates = [Person]()
|
||||
|
||||
init(name:String) {
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func rank(p:Person) -> Int {
|
||||
for (i, candidate) in enumerate(self.candidates) {
|
||||
if candidate === p {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return self.candidates.count + 1
|
||||
}
|
||||
|
||||
func prefers(p:Person) -> Bool {
|
||||
if let fiance = self.fiance {
|
||||
return self.rank(p) < self.rank(fiance)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func nextCandidate() -> Person? {
|
||||
if self.candidateIndex >= self.candidates.count {
|
||||
return nil
|
||||
}
|
||||
return self.candidates[candidateIndex++]
|
||||
}
|
||||
|
||||
func engageTo(p:Person) {
|
||||
p.fiance?.fiance = nil
|
||||
p.fiance = self
|
||||
self.fiance?.fiance = nil
|
||||
self.fiance = p
|
||||
}
|
||||
|
||||
func swapWith(p:Person) {
|
||||
let thisFiance = self.fiance
|
||||
let pFiance = p.fiance
|
||||
println("\(self.name) swapped partners with \(p.name)")
|
||||
if pFiance != nil && thisFiance != nil {
|
||||
self.engageTo(pFiance!)
|
||||
p.engageTo(thisFiance!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isStable(guys:[Person], gals:[Person]) -> Bool {
|
||||
for guy in guys {
|
||||
for gal in gals {
|
||||
if guy.prefers(gal) && gal.prefers(guy) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func engageEveryone(guys:[Person]) {
|
||||
var done = false
|
||||
while !done {
|
||||
done = true
|
||||
for guy in guys {
|
||||
if guy.fiance == nil {
|
||||
done = false
|
||||
if let gal = guy.nextCandidate() {
|
||||
if gal.fiance == nil || gal.prefers(guy) {
|
||||
guy.engageTo(gal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doMarriage() {
|
||||
let abe = Person(name: "Abe")
|
||||
let bob = Person(name: "Bob")
|
||||
let col = Person(name: "Col")
|
||||
let dan = Person(name: "Dan")
|
||||
let ed = Person(name: "Ed")
|
||||
let fred = Person(name: "Fred")
|
||||
let gav = Person(name: "Gav")
|
||||
let hal = Person(name: "Hal")
|
||||
let ian = Person(name: "Ian")
|
||||
let jon = Person(name: "Jon")
|
||||
let abi = Person(name: "Abi")
|
||||
let bea = Person(name: "Bea")
|
||||
let cath = Person(name: "Cath")
|
||||
let dee = Person(name: "Dee")
|
||||
let eve = Person(name: "Eve")
|
||||
let fay = Person(name: "Fay")
|
||||
let gay = Person(name: "Gay")
|
||||
let hope = Person(name: "Hope")
|
||||
let ivy = Person(name: "Ivy")
|
||||
let jan = Person(name: "Jan")
|
||||
|
||||
abe.candidates = [abi, eve, cath, ivy, jan, dee, fay, bea, hope, gay]
|
||||
bob.candidates = [cath, hope, abi, dee, eve, fay, bea, jan, ivy, gay]
|
||||
col.candidates = [hope, eve, abi, dee, bea, fay, ivy, gay, cath, jan]
|
||||
dan.candidates = [ivy, fay, dee, gay, hope, eve, jan, bea, cath, abi]
|
||||
ed.candidates = [jan, dee, bea, cath, fay, eve, abi, ivy, hope, gay]
|
||||
fred.candidates = [bea, abi, dee, gay, eve, ivy, cath, jan, hope, fay]
|
||||
gav.candidates = [gay, eve, ivy, bea, cath, abi, dee, hope, jan, fay]
|
||||
hal.candidates = [abi, eve, hope, fay, ivy, cath, jan, bea, gay, dee]
|
||||
ian.candidates = [hope, cath, dee, gay, bea, abi, fay, ivy, jan, eve]
|
||||
jon.candidates = [abi, fay, jan, gay, eve, bea, dee, cath, ivy, hope]
|
||||
abi.candidates = [bob, fred, jon, gav, ian, abe, dan, ed, col, hal]
|
||||
bea.candidates = [bob, abe, col, fred, gav, dan, ian, ed, jon, hal]
|
||||
cath.candidates = [fred, bob, ed, gav, hal, col, ian, abe, dan, jon]
|
||||
dee.candidates = [fred, jon, col, abe, ian, hal, gav, dan, bob, ed]
|
||||
eve.candidates = [jon, hal, fred, dan, abe, gav, col, ed, ian, bob]
|
||||
fay.candidates = [bob, abe, ed, ian, jon, dan, fred, gav, col, hal]
|
||||
gay.candidates = [jon, gav, hal, fred, bob, abe, col, ed, dan, ian]
|
||||
hope.candidates = [gav, jon, bob, abe, ian, dan, hal, ed, col, fred]
|
||||
ivy.candidates = [ian, col, hal, gav, fred, bob, abe, ed, jon, dan]
|
||||
jan.candidates = [ed, hal, gav, abe, bob, jon, col, ian, fred, dan]
|
||||
|
||||
let guys = [abe, bob, col, dan, ed, fred, gav, hal, ian, jon]
|
||||
let gals = [abi, bea, cath, dee, eve, fay, gay, hope, ivy, jan]
|
||||
|
||||
engageEveryone(guys)
|
||||
|
||||
for guy in guys {
|
||||
println("\(guy.name) is engaged to \(guy.fiance!.name)")
|
||||
}
|
||||
|
||||
println("Stable = \(isStable(guys, gals))")
|
||||
jon.swapWith(fred)
|
||||
println("Stable = \(isStable(guys, gals))")
|
||||
|
||||
}
|
||||
|
||||
doMarriage()
|
||||
Loading…
Add table
Add a link
Reference in a new issue