Created
July 1, 2014 08:19
-
-
Save weidagang/eaf357b6cf36f09bb1f3 to your computer and use it in GitHub Desktop.
HackerRank: List Replication
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
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