Advertisement
Guest User

Untitled

a guest
Sep 12th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.70 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement