Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement this problem in 4 versions: iterative version, recursive version, tail

ID: 3725212 • Letter: I

Question

Implement this problem in 4 versions: iterative version, recursive version, tail-recursive version and map filter reduce version in SCALA programming language:

Write a function that computes the sum of numbers in a list of lists of numbers:

sumOfSums(List(List(1, 2, 3), List(4, 5, 6)) = 21

Explanation / Answer

/* Important: List.sum => sum function return sum of all list elements * 1. Iterative Method: In this approach you can use for loop , the loop will go through each list one by one and you can calculate sum of list using list.sum 2. Recursion: In this approach you can traverse list by list recursively and calculate sum of list in returning phase 3. Tail-recursion: unlike recursion it does not calculate the sum in returning phase while it calculate sum in forwarding phase 5. Map-Reduce: Used map and function only. Sample Input: List(List(1, 2, 3), List(4, 5, 6)) Sample Output: Iterative: 21 Recursive: 21 Tail-Recursive: 21 Using Map and reduce: 21 */ //Functions names are differing you can change name of functions //Create an scala object with name TestMethods.scala and save code from below line to the end of answer //Or copy individual methods to your environment //Your Scala program start here object TestMethods { //Different functions //Version 1: Iterative: using for loop, loop will go through each list def sumOfSumsIterative(listOfLists:List[List[Int]]):Int={ var sum=0 //loop will Iterate on each list , variable list will have a list at time for(listlist.sum).sum //main method def main(args: Array[String]): Unit = { //Declaring list of list of integer type val listOfLists=List(List(1, 2, 3), List(4, 5, 6)) //Calling above all four functions println("Iterative: "+sumOfSumsIterative(listOfLists)) println("Recursive: "+sumOfSumsRecursion(listOfLists)) println("Tail-Recursive: "+sumOfSumsTailRecursion(listOfLists)) println("Using Map and reduce: "+sumOfSums(listOfLists)) } }