Skip to content

Instantly share code, notes, and snippets.

@weidagang
Created July 1, 2014 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weidagang/eaf357b6cf36f09bb1f3 to your computer and use it in GitHub Desktop.
Save weidagang/eaf357b6cf36f09bb1f3 to your computer and use it in GitHub Desktop.
HackerRank: List Replication
package list.replication
object Main {
def repeat[T](num: Int, e: T): List[T] = if (num <= 0) List[T]() else e :: repeat(num - 1, e);
def f(num: Int, arr: List[Int]): List[Int] = arr.flatMap(e => repeat(num, e));
def main(args: Array[String]) {
val lst1 = f(4, List(1, 2, 3));
lst1.map(e => println(e))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment