Advertisement
nikhilbedi

List Replication

Nov 8th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.43 KB | None | 0 0
  1. // This function takes in a List, say {1,2,3,4,5}
  2. // It also takes in an int, say 2
  3. // the returning List[Int] should be filled with {1,1,2,2,3,3,4,4,5,5}
  4.  
  5. def f(num:Int, arr:List[Int]):List[Int] = {
  6.     var x : List[Int] = List()
  7.     arr.foreach(data => x ::: addXTimes(data, num))
  8.     def addXTimes(value:Int, time:Int):List[Int] = {
  9.         var y : List[Int] = List()
  10.         if(time > 0) {
  11.             y ::: addXTimes(value, time-1)
  12.         }
  13.         y
  14.     }
  15.     x
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement