Question: (I need help programming this code. If you can do it in Scala, that would be great. If not, Python works too. Thank you.) Write

(I need help programming this code. If you can do it in Scala, that would be great. If not, Python works too. Thank you.)

Write a tail recursive function sumUpEvenIndices that given a list of integers List((I need help programming this code. If you can do it in) returns the sum of all elements Scala, that would be great. If not, Python works too. Thank you.) at even numbered indices.

Corner Cases

-Empty list must return 0.

-List with just one element must return that element.

Restrictions

-Must be tail recursive.

-Allowed list API functions/operations: list.length, list.head, list.tail.

+All other operations over lists disallowed.

-Do not try to convert the list into an array or some other data structure.

-Use of vars/loops is not allowed.

Note: ??? indicates that there is a missing function or code fragment that needs to be filled in. In scala, it is also a macro that throws a NotImplemented exception. Make sure that you remove the ??? and replace it with the answer.

###CODE

import scala.annotation.tailrec @tailrec // YOUR CODE HERE ???

//BEGIN TEST

val t1 = sumUpEvenIndices(List(5, 10, 12, -3) ) assert( t1 == 17, s"Test1 failed: expected answer 17, got: $t1")

val t2 = sumUpEvenIndices(List() ) assert( t2 == 0, s"Test2 failed: expected answer 0, got: $t2")

val t3 = sumUpEvenIndices(List(-3) ) assert( t3 == -3, s"Test3 failed: expected answer -3, got: $t3")

val t4 = sumUpEvenIndices(List(0, 1, 0, 5, 0, 9, 0, 12, 0, -45, 0, -56) ) assert( t4 == 0, s"Test4 failed: expected answer 0, got: $t4")

val t5 = sumUpEvenIndices(List(11, 0) ) assert( t5 == 11, s"Test5 failed: expected answer 11, got: $t5")

passed(7)

//END TEST

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!