Created
December 25, 2012 21:49
-
-
Save quii/4375599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util.Random | |
/* | |
Takes a sequence of secret santa participants and suggests who should give to who | |
*/ | |
object SecretSanta { | |
def main(args: Array[String]){ | |
val people = args.toSet | |
if (people.size<3){ | |
println("Secret santa with less than 3 people is silly") | |
sys.exit() | |
} | |
val pairs = scala.collection.mutable.Map[String, String]() | |
people foreach { person => | |
def getPersonToGiveTo(candidates: Set[String]): Option[String] = { | |
def notBeingGivenYet(p: String): Boolean = !pairs.values.exists(_==p) | |
def notGivingToMe(p: String): Boolean = pairs.get(p) match{ | |
case Some(receiver) if receiver==person => false | |
case _ => true | |
} | |
Random.shuffle(candidates filter notBeingGivenYet filter notGivingToMe).headOption | |
} | |
val peopleNotMe = people.filterNot(_ == person) | |
getPersonToGiveTo(peopleNotMe) match{ | |
case Some(luckyPerson) => pairs += person -> luckyPerson | |
case None => {} | |
} | |
} | |
println("Secret santa happy times! \n") | |
pairs foreach { happyCouple => | |
println("%s should give to %s".format(happyCouple._1, happyCouple._2)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment