Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Last active August 29, 2015 14:24
Show Gist options
  • Save Blaisorblade/07e0b2d39e5497914dbc to your computer and use it in GitHub Desktop.
Save Blaisorblade/07e0b2d39e5497914dbc to your computer and use it in GitHub Desktop.
// Input param: a set of IDs of participants.
val origChapters = List(6, 7, 5, 11, 8, 9, 15, 17, 24, 21, 31, 22, 27, 23, 25, 29)
val nReviewers = 2
// Output: assign nReviewers reviewer IDs to each submitter ID, so that the two reviewers and the reviewee are all different, and such that everybody does nReviewers reviews.
val chapters = origChapters.sorted
val r = new util.Random
def rndIdx() = r.nextInt(chapters.length)
case class ReviewAssignment(reviewers: List[Int], target: Int)
def multiply[T](l: List[T], n: Int) = List.fill(n)(l).flatten
def getPairs() =
r.shuffle(multiply(chapters, nReviewers)).grouped(nReviewers).toList.zipWithIndex map {
case (revs, idx) => ReviewAssignment(revs, chapters(idx))
}
def areDistinct[T](l: T*) = l.toSet.size == l.size
def checkPairs(reviewers: List[ReviewAssignment]) =
reviewers forall {
case ReviewAssignment(List(rev1, rev2), tgt) => areDistinct(rev1, rev2, tgt)
}
def reviewers(): List[ReviewAssignment] = {
val cand = getPairs()
if (checkPairs(cand))
cand
else
reviewers()
}
def checkSanity(l: List[ReviewAssignment]) = {
l.flatMap (_.reviewers).sorted == multiply(chapters, nReviewers).sorted
}
def output(res: List[ReviewAssignment]) = {
res map { case ReviewAssignment(List(a, b), tgt) => s"$tgt $a $b" } mkString ("\n")
}
def genMain() = {
val res = reviewers()
assert(checkSanity(res))
println(output(res))
res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment