Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.90 KB | None | 0 0
  1. import scala.annotation.tailrec
  2.  
  3. object Test {
  4.   def main(args: Array[String]): Unit = {
  5.     @tailrec def mmerge(ints: List[Int], ints1: List[Int], out: List[Int] = List()): List[Int] ={
  6.       var tout = out
  7.       if(ints.isEmpty && ints1.isEmpty) return out
  8.       else{
  9.         if(ints.isEmpty){
  10.           tout = tout ++ List(ints1.head)
  11.         } else if(ints1.isEmpty){
  12.           tout = tout ++ List(ints.head)
  13.         } else if(ints.head >= ints1.head){
  14.           tout = tout ++ List(ints.head)
  15.         } else{
  16.           tout = tout ++ List(ints1.head)
  17.         }
  18.         return mmerge(ints.tail,ints1.tail,tout)
  19.       }
  20.     }
  21.  
  22.     // example to work with
  23.     val testList1 = List(9,5,2,1,-5)
  24.     val testList2 = List(15,9,5,-1,-90)
  25.     val mergedTestList = mmerge( testList1, testList2 )
  26.     println( mergedTestList.mkString(" ") )
  27.     // result
  28.     //15 9 9 5 5 2 1 -1 -5 -90
  29.  
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement