Created
December 11, 2013 17:46
-
-
Save rkrzewski/7915056 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 scala.util.Random | |
object S99_P23 { | |
def removeAt[T](n: Int, ts: Seq[T]) = (ts.take(n) ++ ts.drop(n + 1), ts(n)) | |
def randomSelect[T](count: Int, ts: Seq[T]): Seq[T] = { | |
val r = new Random | |
def go(count: Int, ts: Seq[T], acc: Seq[T]): Seq[T] = { | |
if (count > 0) { | |
val (next, elem) = removeAt(r.nextInt(ts.length), ts) | |
go(count - 1, next, elem +: acc) | |
} | |
else acc | |
} | |
go(count, ts, Seq.empty[T]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment