Anonymous user
Stable marriage problem: Difference between revisions
→{{header|Scala}}: simplification of method signatures
Thundergnat (talk | contribs) m (Reverted edits by Simple9371 (talk) to last revision by Jolkdarr) |
(→{{header|Scala}}: simplification of method signatures) |
||
Line 5,654:
=={{header|Scala}}==
{{trans|Java}}
<lang scala>
import scala.collection.mutable.HashMap▼
def run() {
val matches = matching
matches.foreach { e => println(s"${e._1} is engaged to ${e._2}") }
if (checkMatches(
println("Marriages are stable")
else
Line 5,675 ⟶ 5,669:
matches += girl2 -> tmp
println(girl1 + " and " + girl2 + " have switched partners")
if (checkMatches(
println("Marriages are stable")
else
Line 5,681 ⟶ 5,675:
}
private def matching(guys: Seq[String],▼
private type TM =
val
val freeGuys = scala.collection.mutable.Queue.empty ++
while (freeGuys.nonEmpty) {
val guy = freeGuys.dequeue()
Line 5,711 ⟶ 5,705:
}
private def checkMatches(
if (!girls.toSet.subsetOf(matches.keySet) || !guys.toSet.subsetOf(
▲ if (!girls.toSet.subsetOf(matches.keySet) || !guys.toSet.subsetOf(HashSet() ++= matches.values))
return false
val invertedMatches = new
matches.foreach { invertedMatches += _.swap }
for ((k, v) <- matches) {
val shePrefers = girlPrefers(k)
val sheLikesBetter
val hePrefers = guyPrefers(v)
val heLikesBetter
for (guy <- sheLikesBetter) {
Line 5,750 ⟶ 5,741:
private val guys = "abe" :: "bob" :: "col" :: "dan" :: "ed" :: "fred" :: "gav" :: "hal" :: "ian" :: "jon" :: Nil
private val girls = "abi" :: "bea" :: "cath" :: "dee" :: "eve" :: "fay" :: "gay" :: "hope" :: "ivy" :: "jan" :: Nil
private val guyPrefers =
"bob" -> List("cath", "hope", "abi", "dee", "eve", "fay", "bea", "jan", "ivy", "gay"),
"col" -> List("hope", "eve", "abi", "dee", "bea", "fay", "ivy", "gay", "cath", "jan"),
Line 5,760 ⟶ 5,751:
"ian" -> List("hope", "cath", "dee", "gay", "bea", "abi", "fay", "ivy", "jan", "eve"),
"jon" -> List("abi", "fay", "jan", "gay", "eve", "bea", "dee", "cath", "ivy", "hope"))
private val girlPrefers =
"bea" -> List("bob", "abe", "col", "fred", "gav", "dan", "ian", "ed", "jon", "hal"),
"cath" -> List("fred", "bob", "ed", "gav", "hal", "col", "ian", "abe", "dan", "jon"),
|