Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object tp1 {
- def encode(l : List[Any], consecutiveOccurence : Int = 1) : List[Any] = l match {
- case Nil => ???
- case x::Nil => List((x,consecutiveOccurence))
- case x::y::z => if(x==y){
- encode(y::z,consecutiveOccurence + 1)
- }
- else{
- List((x,consecutiveOccurence)) ::: encode(y::z,1)
- }
- }
- def main(args: Array[String]) {
- //println(penultimate(List(1,2,3)))
- //println(listLength(List(1,2,3)))
- //println(flattenNestedList(List(List(1, 1), List(2), List(3), List(5, 8))))
- //println(duplicateElementList(List(1,2)))
- //println(dropElementByIndex(3,List(1,2,3,4,5,6)))
- println(encode(List(1,2,2,2,2,3,3,3,2)))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement