SHARE
TWEET

Untitled

a guest Sep 12th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. object tp1 {
  2.  
  3.   def encode(l : List[Any], consecutiveOccurence : Int = 1) : List[Any] = l match {
  4.     case Nil => ???
  5.     case x::Nil => List((x,consecutiveOccurence))
  6.     case x::y::z => if(x==y){
  7.       encode(y::z,consecutiveOccurence + 1)
  8.     }
  9.     else{
  10.       List((x,consecutiveOccurence)) ::: encode(y::z,1)
  11.     }
  12.   }
  13.  
  14.   def main(args: Array[String]) {
  15.         //println(penultimate(List(1,2,3)))
  16.         //println(listLength(List(1,2,3)))
  17.         //println(flattenNestedList(List(List(1, 1), List(2), List(3), List(5, 8))))
  18.         //println(duplicateElementList(List(1,2)))
  19.         //println(dropElementByIndex(3,List(1,2,3,4,5,6)))
  20.         println(encode(List(1,2,2,2,2,3,3,3,2)))
  21.     }
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top