Stable marriage problem: Difference between revisions

Content added Content deleted
(Added EchoLisp)
(Kotlin version updated)
Line 3,281: Line 3,281:
val names: List<String> by lazy { map.keys.toList() }
val names: List<String> by lazy { map.keys.toList() }


fun preferences(k: String, v: String): LinkedList<String> {
fun preferences(k: String, v: String): List<String> {
val likesBetter = LinkedList<String>()
val prefers = get(k)!!
val prefers = get(k)!!
likesBetter.addAll(prefers.slice(0..prefers.indexOf(v)))
return ArrayList<String>(prefers.slice(0..prefers.indexOf(v)))
return likesBetter
}
}
}
}
Line 3,291: Line 3,289:
class EngagementRegistry() : TreeMap<String, String>() {
class EngagementRegistry() : TreeMap<String, String>() {
constructor(guys: People, girls: People) : this() {
constructor(guys: People, girls: People) : this() {
val freeGuys = guys.names.toArrayList()
val freeGuys = guys.names.toMutableList()
while (freeGuys.any()) {
while (freeGuys.any()) {
val guy = freeGuys.removeAt(0) //get a load of THIS guy
val guy = freeGuys.removeAt(0) // get a load of THIS guy
val guy_p = guys[guy]!!
val guy_p = guys[guy]!!
for (girl in guy_p)
for (girl in guy_p)
Line 3,317: Line 3,315:
}
}


fun checkStability(guys: People, girls: People) {
fun analyse(guys: People, girls: People) {
if (check(guys, girls))
if (check(guys, girls))
println("Marriages are stable")
println("Marriages are stable")
Line 3,393: Line 3,391:
val matches = EngagementRegistry(guys, girls)
val matches = EngagementRegistry(guys, girls)
print(matches)
print(matches)
matches.checkStability(guys, girls)
matches.analyse(guys, girls)
matches.swap(girls, 0, 1)
matches.swap(girls, 0, 1)
matches.checkStability(guys, girls)
matches.analyse(guys, girls)
}</lang>
}</lang>
{{out}}
{{out}}