Q:

Write a Scala program to triplicate each element immediately next to the given list of integers

0

Write a Scala program to triplicate each element immediately next to the given list of integers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_List {    
 def duplicate[A](o_list:List[A]):List[A] = {
    o_list flatMap { element => List(element, element,element) }
    }  
  def main(args: Array[String]): Unit = {
     val nums = List(1,2,3,3,4,5,6,7)
     println("Original List:")  
     println(nums)
     val duplicate_list1 = duplicate(nums)
     println("New list after triplicating each element immediately next to the said list:")
     println(duplicate_list1)     
    }
}

Sample Output:

Original List:
List(1, 2, 3, 3, 4, 5, 6, 7)
New list after triplicating each element immediately next to the said list:
List(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now