Question: :(Need In Scala) 2C (7 Points): Convert A List Into An Indexed List. Write A Function To Convert A List Of Strings Into An Indexed

:(Need In Scala) 2C (7 Points): Convert A List Into An Indexed List. Write A Function To Convert A List Of Strings Into An Indexed List Of Strings. Indices Start At 0. As An Example: Input List("Hello", "World", "My", "Cat", "Is", "Grumpy", "Today") Output List( (0, "Hello"), (1, "World"), (2, "My"), (3,"Cat"), (4, "Is"), (5, "Grumpy"), (6,"Today") ) You Are

(need in scala)

2C (7 points): Convert a list into an indexed list.

Write a function to convert a list of strings into an indexed list of strings. Indices start at 0. As an example:

InputList("hello", "world", "my", "cat", "is", "grumpy", "today")

OutputList( (0, "hello"), (1, "world"), (2, "my"), (3,"cat"), (4, "is"), (5, "grumpy"), (6,"today") )

You are allowed to use just the basic list operatios such as cons of an element to a list (::) and concatenation of two lists (++, or ::: operators). List API functions reverse, map, filter, foldLeft and foldRight but not other list API functions. Do not usevar,loopsorrecursion.

In [ ]:

def makeIndexedList(lst:List[String]): List[(Int, String)] = {

??? // YOUR CODE HERE

}

In [ ]:

// BEGIN TEST

val t1 = makeIndexedList(List("hello"))

assert(t1 == List((0,"hello")), s"Test 1 failed - your code returned $t1")

val t2 = makeIndexedList(List("hello", "world"))

assert(t2 == List((0,"hello"), (1, "world")), s"Test 2 failed - your code returned $t2")

val t3 = makeIndexedList(Nil)

assert(t3 == Nil, s"Test 3 failed - your code returned $t3")

val t4 = makeIndexedList(List("a","b","c","d","e"))

assert(t4 == List((0,"a"), (1,"b"), (2,"c"), (3,"d"), (4,"e")), s"Test 4 failed - your code returned $t4")

passed(8)

// 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 Programming Questions!